iOS開發(fā)實(shí)現(xiàn)轉(zhuǎn)盤功能
本文實(shí)例為大家分享了iOS實(shí)現(xiàn)轉(zhuǎn)盤功能的具體代碼,供大家參考,具體內(nèi)容如下
今天給同學(xué)們講解一下一個(gè)轉(zhuǎn)盤選號的功能,直接上代碼直接看

ViewController
#pragma mark - 如果要旋轉(zhuǎn)那就第一考慮錨點(diǎn) 核心動(dòng)畫看到的都是假象 真實(shí)的位置并沒有發(fā)生改變
//
// ViewController.m
// 5-網(wǎng)易轉(zhuǎ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)盤的實(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)盤的實(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í)候會調(diào)用,但是并沒有連好線
//- (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)聽按鈕的點(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 - 開始旋轉(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)擊開始選號的時(shí)候
- (IBAction)startPicker:(id)sender {
// 不需要定時(shí)器旋轉(zhuǎn)
self.link.paused = YES;
// 中間的轉(zhuǎ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ù),
// 通過transform獲取角度
CGFloat angle = atan2(_selBtn.transform.b, _selBtn.transform.a);
// 旋轉(zhuǎn)轉(zhuǎ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)盤的實(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
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
iOS開發(fā)檢測是否開啟定位、是否允許消息推送等權(quán)限的實(shí)例
下面小編就為大家分享一篇iOS開發(fā)檢測是否開啟定位、是否允許消息推送等權(quán)限的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01
實(shí)例分析IOS實(shí)現(xiàn)自動(dòng)打包
本篇文章給大家分享了IOS實(shí)現(xiàn)自動(dòng)打包的相關(guān)知識點(diǎn),以及需要的操作內(nèi)容做了分享,有需要的朋友可以學(xué)習(xí)下。2018-05-05
iOS開發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤自定義按鍵
這篇文章主要介紹了iOS開發(fā)之UIKeyboardTypeNumberPad數(shù)字鍵盤自定義按鍵 的相關(guān)資料,需要的朋友可以參考下2016-08-08
Objective-C優(yōu)雅使用KVO觀察屬性值變化
這篇文章主要為大家介紹了Objective-C優(yōu)雅使用KVO觀察屬性值變化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
iOS開發(fā)實(shí)戰(zhàn)之Label全方位對齊的輕松實(shí)現(xiàn)
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)實(shí)戰(zhàn)之輕松實(shí)現(xiàn)Label全方位對齊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-10-10
iOS應(yīng)用開發(fā)中UITableView的分割線的一些設(shè)置技巧
這篇文章主要介紹了iOS應(yīng)用開發(fā)中UITableView分割線的一些設(shè)置技巧,包括消除分割線的方法,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-03-03
IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器
這篇文章主要介紹了IOS利用CocoaHttpServer搭建手機(jī)本地服務(wù)器的步驟,幫助大家更好的理解和學(xué)習(xí)使用ios開發(fā),感興趣的朋友可以了解下2021-04-04
iOS App開發(fā)中的UISegmentedControl分段組件用法總結(jié)
UISegmentedControl主要被用來制作分頁按鈕或添加跳轉(zhuǎn)到不同位置的標(biāo)簽,這里我們就來看一下iOS App開發(fā)中的UISegmentedControl分段組件用法總結(jié),需要的朋友可以參考下2016-06-06
iOS利用攝像頭獲取環(huán)境光感參數(shù)的方法
本篇文章主要介紹了iOS利用攝像頭獲取環(huán)境光感參數(shù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11

