亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

iOS實現(xiàn)轉(zhuǎn)盤效果

瀏覽:22日期:2022-09-17 10:10:29

本文實例為大家分享了iOS實現(xiàn)轉(zhuǎn)盤效果的具體代碼,供大家參考,具體內(nèi)容如下

Demo下載地址: iOS轉(zhuǎn)盤效果

功能:實現(xiàn)了常用的iOS轉(zhuǎn)盤效果,輪盤抽獎效果的實現(xiàn),轉(zhuǎn)盤可以暫停,旋轉(zhuǎn),已經(jīng)快速旋轉(zhuǎn)抽獎,選中的效果指向正上方。

效果圖:

iOS實現(xiàn)轉(zhuǎn)盤效果

工程文件目錄:

iOS實現(xiàn)轉(zhuǎn)盤效果

核心代碼:

//// ViewController.m// 轉(zhuǎn)盤效果//// Created by llkj on 2017/8/31.// Copyright © 2017年 LayneCheung. All rights reserved.//#import 'ViewController.h'#import 'WheelView.h'@interface ViewController ()@property (nonatomic, weak) WheelView *wheelV;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; WheelView *wheelV = [WheelView wheelView]; wheelV.center = self.view.center; self.wheelV = wheelV; [self.view addSubview:wheelV];}- (IBAction)rotation:(id)sender { [self.wheelV rotation];}- (IBAction)stop:(id)sender { [self.wheelV stop];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end

WheelView文件

//// WheelView.h// 轉(zhuǎn)盤效果//// Created by llkj on 2017/8/31.// Copyright © 2017年 LayneCheung. All rights reserved.//#import <UIKit/UIKit.h>@interface WheelView : UIView+ (instancetype)wheelView;- (void)rotation;- (void)stop;@end

//// WheelView.m// 轉(zhuǎn)盤效果//// Created by llkj on 2017/8/31.// Copyright © 2017年 LayneCheung. All rights reserved.//#import 'WheelView.h'#import 'WheelBtn.h'#define angle2Rad(angle) ((angle) / 180.0 * M_PI)@interface WheelView ()<CAAnimationDelegate>@property (weak, nonatomic) IBOutlet UIImageView *contentV;@property (nonatomic, weak) UIButton *selectBtn;@property (nonatomic, strong) CADisplayLink *link;@end@implementation WheelView- (CADisplayLink *)link { if (_link == nil) { CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)]; [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; _link = link; } return _link;}+ (instancetype)wheelView { return [[[NSBundle mainBundle] loadNibNamed:@'WheelView' owner:nil options:nil] lastObject];}- (instancetype)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self = [[[NSBundle mainBundle] loadNibNamed:@'WheelView' owner:nil options:nil] lastObject]; } return self;}- (void)awakeFromNib { [super awakeFromNib]; CGFloat btnW = 68; CGFloat btnH = 143; CGFloat angle = 0; //加載原始大圖片 UIImage *oriImage = [UIImage imageNamed:@'LuckyAstrology']; //加載原始選中的大圖片 UIImage *oriSelImg = [UIImage imageNamed:@'LuckyAstrologyPressed']; CGFloat X = 0; CGFloat Y = 0; CGFloat sacle = [UIScreen mainScreen].scale; CGFloat clipW = oriImage.size.width / 12.0 * sacle; CGFloat clipH = oriImage.size.height * sacle; for (int i = 0; i < 12; i ++) { WheelBtn *btn = [WheelBtn buttonWithType:UIButtonTypeCustom]; btn.bounds = CGRectMake(0, 0, btnW, btnH); //按鈕正常狀態(tài)圖片 X = i * clipW; //給定一張圖片截取指定區(qū)域內(nèi)的圖片 CGImageRef clipImg = CGImageCreateWithImageInRect(oriImage.CGImage, CGRectMake(X, Y, clipW, clipH)); [btn setImage:[UIImage imageWithCGImage:clipImg] forState:UIControlStateNormal]; //按鈕選中狀態(tài)圖片 CGImageRef clipSelImg = CGImageCreateWithImageInRect(oriSelImg.CGImage, CGRectMake(X, Y, clipW, clipH)); [btn setImage:[UIImage imageWithCGImage:clipSelImg] forState:UIControlStateSelected]; [btn setBackgroundImage:[UIImage imageNamed:@'LuckyRototeSelected'] forState:UIControlStateSelected]; btn.layer.anchorPoint = CGPointMake(0.5, 1); btn.layer.position = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5); btn.transform = CGAffineTransformMakeRotation(angle2Rad(angle)); angle += 30; [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.contentV addSubview:btn]; //默認(rèn)第一個按鈕選中 if (i == 0) { [self btnClick:btn]; } }}- (void)btnClick:(UIButton *)btn { //1.讓當(dāng)前選中的按鈕取消選中 self.selectBtn.selected = NO; //2.讓當(dāng)前點擊的按鈕成為選中狀態(tài) btn.selected = YES; //3.當(dāng)前點擊的按鈕成為選中狀態(tài) self.selectBtn = btn;}- (void)rotation { self.link.paused = NO;}- (void)stop { self.link.paused = YES;}- (void)update { self.contentV.transform = CGAffineTransformRotate(self.contentV.transform, M_PI / 300.0);}- (IBAction)start:(id)sender { //快速轉(zhuǎn)幾圈 CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @'transform.rotation'; anim.toValue = @(M_PI * 4); anim.duration = 0.5; anim.repeatCount = 1; anim.delegate = self; [self.contentV.layer addAnimation:anim forKey:nil];}- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { CGAffineTransform transform = self.selectBtn.transform; //獲取當(dāng)前選中按鈕的旋轉(zhuǎn)角度 CGFloat angle = atan2(transform.b, transform.a); //動畫結(jié)束時當(dāng)前選中的按鈕指向最上方 self.contentV.transform = CGAffineTransformMakeRotation(-angle);}@end

WheelBtn.m

//// WheelBtn.m// 轉(zhuǎn)盤效果//// Created by llkj on 2017/8/31.// Copyright © 2017年 LayneCheung. All rights reserved.//#import 'WheelBtn.h'@implementation WheelBtn- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { CGRect rect = CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height * 0.5); if (CGRectContainsPoint(rect, point)) { // 在指定的范圍內(nèi) return [super hitTest:point withEvent:event]; } else { return nil; }}//取消按鈕高亮狀態(tài)做的事- (void)setHighlighted:(BOOL)highlighted {}//返回當(dāng)前按鈕中的image位置和尺寸- (CGRect)imageRectForContentRect:(CGRect)contentRect { return CGRectMake((contentRect.size.width - 40) *0.5, 20, 40, 48);}//返回當(dāng)前按鈕中的Label位置和尺寸//- (CGRect)titleRectForContentRect:(CGRect)contentRect{////}@end

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: IOS
相關(guān)文章:
主站蜘蛛池模板: 国产精品嫩草视频永久网址 | www.毛片在线观看 | 97理论三级九七午夜在线观看 | 亚洲毛片在线播放 | 久青草青综合在线视频 | 狠狠色婷婷综合天天久久丁香 | 视频一区二区国产 | 伊人色婷婷综在合线亚洲 | 午夜一区二区免费视频 | 1024在线观看国产天堂 | 欧美日韩国产免费一区二区三区 | 亚洲精品一区久久狠狠欧美 | 亚洲色在线视频 | 国产高清色视频免费看的网址 | www.一区 | 亚洲精品久久久久久动漫剧情 | 六月婷婷精品视频在线观看 | 免费观看三级毛片 | 久久精品国产91久久综合麻豆自制 | 97精品在线视频 | 免费大片免费观看 | 欧美人伦禁忌dvd | 欧美国产在线观看 | 在线中文天堂 | 国产精品久久在线 | 日韩欧美在线视频观看 | 玖玖99 | 日本成人网址 | 欧美一区二区三区精品影视 | 亚洲综合久久久久久888 | 日本成人网址 | 久久在现 | 亚洲精品欧美综合四区 | 免费观看的黄色 | 天天在线天天看成人免费视频 | 视频黄在线观看 | 91短视频版在线观看www免费 | 高清国产在线播放成人 | 国产欧美国产精品第二区 | 99久久一香蕉国产线看观看 | 色香影院|