iOS開(kāi)發(fā)實(shí)現(xiàn)轉(zhuǎn)盤(pán)功能
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)轉(zhuǎn)盤(pán)功能的具體代碼,供大家參考,具體內(nèi)容如下
今天給同學(xué)們講解一下一個(gè)轉(zhuǎn)盤(pán)選號(hào)的功能,直接上代碼直接看
ViewController
#pragma mark - 如果要旋轉(zhuǎn)那就第一考慮錨點(diǎn) 核心動(dòng)畫(huà)看到的都是假象 真實(shí)的位置并沒(méi)有發(fā)生改變 // // ViewController.m // 5-網(wǎng)易轉(zhuǎn)盤(pán)的實(shí)現(xiàn) // // Created by Jordan zhou on 2018/10/10. // Copyright © 2018年 Jordan zhou. All rights reserved. // #import "ViewController.h" #import "ZZWheelView.h" @interface ViewController () /** 展示的view */ @property (nonatomic, strong) ZZWheelView *wheelView; @end @implementation ViewController - (IBAction)start:(id)sender { [self.wheelView start]; } - (IBAction)stop:(id)sender { [self.wheelView pause]; } #pragma mark - 懶加載 - (ZZWheelView *)wheelView { if (!_wheelView) { _wheelView = [ZZWheelView wheelView]; _wheelView.center = self.view.center; } return _wheelView; } - (void)viewDidLoad { [super viewDidLoad]; [self.view addSubview:self.wheelView]; } @end
ZZWheelView
// // ZZWheelView.h // 5-網(wǎng)易轉(zhuǎn)盤(pán)的實(shí)現(xiàn) // // Created by Jordan zhou on 2018/10/10. // Copyright © 2018年 Jordan zhou. All rights reserved. // #import <UIKit/UIKit.h> @interface ZZWheelView : UIView + (instancetype)wheelView; - (void)start; - (void)pause; @end // ZZWheelView.m // 5-網(wǎng)易轉(zhuǎn)盤(pán)的實(shí)現(xiàn) // // Created by Jordan zhou on 2018/10/10. // Copyright © 2018年 Jordan zhou. All rights reserved. // #import "ZZWheelView.h" #import "ZZWheelButton.h" @interface ZZWheelView()<CAAnimationDelegate> @property (weak, nonatomic) IBOutlet UIImageView *centerView; @property (nonatomic, weak) UIButton *selBtn; @property (nonatomic, strong) CADisplayLink *link; @end @implementation ZZWheelView #pragma mark - 懶加載 - (CADisplayLink *)link { if (_link == nil) { _link = [CADisplayLink displayLinkWithTarget:self selector:@selector(angleChange)]; [_link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode]; } return _link; } + (instancetype)wheelView { return [[NSBundle mainBundle] loadNibNamed:@"ZZWheelView" owner:nil options:nil][0]; } #warning - 注意這個(gè)方法只是加載xib的時(shí)候會(huì)調(diào)用,但是并沒(méi)有連好線 //- (instancetype)initWithCoder:(NSCoder *)aDecoder //{ // if (self = [super initWithCoder:aDecoder]) { // NSLog(@"-%@",_centerView); // } // return self; //} - (void)awakeFromNib { [super awakeFromNib]; _centerView.userInteractionEnabled = YES; CGFloat btnW = 68; CGFloat btnH = 143; CGFloat wh = self.bounds.size.width; // 加載大圖片 UIImage *bigImage = [UIImage imageNamed:@"LuckyAstrology"]; // 加載大圖片 UIImage *selBigImage = [UIImage imageNamed:@"LuckyAstrologyPressed"]; // 獲取當(dāng)前使用的圖片像素和點(diǎn)的比例 CGFloat scale = [UIScreen mainScreen].scale; CGFloat imageW = bigImage.size.width / 12 * scale; CGFloat imageH = bigImage.size.height * scale; // CGImageRef image:需要裁減的圖片 // rect:裁減區(qū)域 // 裁減區(qū)域是以像素為基準(zhǔn) // CGImageCreateWithImageInRect(CGImageRef image, CGRect rect) // 添加按鈕 for (int i = 0; i < 12; i++) { ZZWheelButton *btn = [ZZWheelButton buttonWithType:UIButtonTypeCustom]; // 設(shè)置按鈕的位置 btn.layer.anchorPoint = CGPointMake(0.5, 1); btn.bounds = CGRectMake(0, 0, btnW, btnH); btn.layer.position = CGPointMake(wh * 0.5, wh * 0.5); // 按鈕的旋轉(zhuǎn)角度 CGFloat radion = (30 * i) / 180.0 * M_PI; btn.transform = CGAffineTransformMakeRotation(radion); [_centerView addSubview:btn]; // 加載按鈕的圖片 // 計(jì)算裁減區(qū)域 CGRect clipR = CGRectMake(i * imageW, 0, imageW, imageH); // 裁減圖片 CGImageRef imgR = CGImageCreateWithImageInRect(bigImage.CGImage, clipR); UIImage *image = [UIImage imageWithCGImage:imgR]; // 設(shè)置按鈕的圖片 [btn setImage:image forState:UIControlStateNormal]; // 設(shè)置選中狀態(tài)下圖片 imgR = CGImageCreateWithImageInRect(selBigImage.CGImage, clipR); image = [UIImage imageWithCGImage:imgR]; // 設(shè)置按鈕的圖片 [btn setImage:image forState:UIControlStateSelected]; // 設(shè)置選中背景圖片 [btn setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected]; // 監(jiān)聽(tīng)按鈕的點(diǎn)擊 [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; // 默認(rèn)選中第一個(gè) if (i == 0) { [self btnClick:btn]; } } } - (void)btnClick:(UIButton *)btn { _selBtn.selected = NO; btn.selected = YES; _selBtn = btn; } #pragma mark - 開(kāi)始旋轉(zhuǎn) - (void)start { self.link.paused = NO; } // 1.搞個(gè)定時(shí)器,每隔一段時(shí)間就旋轉(zhuǎn)一定的角度,1秒旋轉(zhuǎn)45° #pragma mark - 暫停旋轉(zhuǎn) - (void)pause { self.link.paused = YES; } #pragma mark - 每隔一段時(shí)間旋轉(zhuǎn)一定的角度 - (void)angleChange { // 每一次調(diào)用旋轉(zhuǎn)多少 45 \ 60.0 CGFloat angle = (45 / 60.0) * M_PI / 180.0; _centerView.transform = CGAffineTransformRotate(_centerView.transform, angle); } #pragma mark - 點(diǎn)擊開(kāi)始選號(hào)的時(shí)候 - (IBAction)startPicker:(id)sender { // 不需要定時(shí)器旋轉(zhuǎn) self.link.paused = YES; // 中間的轉(zhuǎn)盤(pán)快速的旋轉(zhuǎn),并且不需要與用戶交互 CABasicAnimation *anim = [CABasicAnimation animation]; anim.keyPath = @"transform.rotation"; anim.toValue = @(M_PI * 2 * 3); anim.duration = 0.5; anim.delegate = self; [_centerView.layer addAnimation:anim forKey:nil]; // 點(diǎn)擊哪個(gè)星座,就把當(dāng)前星座指向中心點(diǎn)上面 // M_PI 3.14 // 根據(jù)選中的按鈕獲取旋轉(zhuǎn)的度數(shù), // 通過(guò)transform獲取角度 CGFloat angle = atan2(_selBtn.transform.b, _selBtn.transform.a); // 旋轉(zhuǎn)轉(zhuǎn)盤(pán) _centerView.transform = CGAffineTransformMakeRotation(-angle); } - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ self.link.paused = NO; }); } @end
ZZWheelButton
// // ZZWheelButton.m // 5-網(wǎng)易轉(zhuǎn)盤(pán)的實(shí)現(xiàn) // // Created by Jordan zhou on 2018/10/10. // Copyright © 2018年 Jordan zhou. All rights reserved. // #import "ZZWheelButton.h" @implementation ZZWheelButton // 尋找最合適的view - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { CGFloat btnW = self.bounds.size.width; CGFloat btnH = self.bounds.size.height; CGFloat x = 0; CGFloat y = btnH / 2; CGFloat w = btnW; CGFloat h = y; CGRect rect = CGRectMake(x, y, w, h); if (CGRectContainsPoint(rect, point)) { return nil; }else{ return [super hitTest:point withEvent:event]; } } // 設(shè)置UIImageView的尺寸 // contentRect:按鈕的尺寸 - (CGRect)imageRectForContentRect:(CGRect)contentRect { // 計(jì)算UIImageView控件尺寸 CGFloat imageW = 40; CGFloat imageH = 46; CGFloat imageX = (contentRect.size.width - imageW) * 0.5; CGFloat imageY = 20; return CGRectMake(imageX, imageY, imageW, imageH); } // 取消高亮狀態(tài) - (void)setHighlighted:(BOOL)highlighted{} @end
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS如何優(yōu)雅地實(shí)現(xiàn)序列動(dòng)畫(huà)詳解
這篇文章主要給大家介紹了關(guān)于iOS如何優(yōu)雅地實(shí)現(xiàn)序列動(dòng)畫(huà)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12iOS?WKWebView秒開(kāi)方案實(shí)戰(zhàn)記錄
從iOS8開(kāi)始,就引入了新的瀏覽器控件WKWebView,用于取代UIWebView,下面這篇文章主要給大家介紹了關(guān)于iOS?WKWebView秒開(kāi)方案的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12iOS開(kāi)發(fā)中UIPopoverController的使用詳解
這篇文章主要介紹了iOS開(kāi)發(fā)中UIPopoverController的使用,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11UIImage加載圖片Images.xcassets加載方法的影響
這篇文章主要介紹了UIImage加載圖片Images.xcassets加載方法的影響的相關(guān)資料,需要的朋友可以參考下2016-12-12學(xué)習(xí)iOS開(kāi)關(guān)按鈕UISwitch控件
這篇文章主要為大家詳細(xì)介紹了iOS開(kāi)關(guān)按鈕UISwitch控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08