今天給同學們講解一下一個轉(zhuǎn)盤選號的功能那么廢話不多說直接上代碼直接看~
iOS開發(fā)-網(wǎng)易轉(zhuǎn)盤.gif
ViewController
#pragma mark - 如果要旋轉(zhuǎn)那就第一考慮錨點 核心動畫看到的都是假象 真實的位置并沒有發(fā)生改變
//
// ViewController.m
// 5-網(wǎng)易轉(zhuǎn)盤的實現(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
CDEBBA1F-4159-46FC-8FC7-FD3A31576845.png
//
// ZZWheelView.h
// 5-網(wǎng)易轉(zhuǎn)盤的實現(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)盤的實現(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 - 注意這個方法只是加載xib的時候會調(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"];
// 獲取當前使用的圖片像素和點的比例
CGFloat scale = [UIScreen mainScreen].scale;
CGFloat imageW = bigImage.size.width / 12 * scale;
CGFloat imageH = bigImage.size.height * scale;
// CGImageRef image:需要裁減的圖片
// rect:裁減區(qū)域
// 裁減區(qū)域是以像素為基準
// CGImageCreateWithImageInRect(CGImageRef image, CGRect rect)
// 添加按鈕
for (int i = 0; i < 12; i++) {
ZZWheelButton *btn = [ZZWheelButton buttonWithType:UIButtonTypeCustom];
// 設置按鈕的位置
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];
// 加載按鈕的圖片
// 計算裁減區(qū)域
CGRect clipR = CGRectMake(i * imageW, 0, imageW, imageH);
// 裁減圖片
CGImageRef imgR = CGImageCreateWithImageInRect(bigImage.CGImage, clipR);
UIImage *image = [UIImage imageWithCGImage:imgR];
// 設置按鈕的圖片
[btn setImage:image forState:UIControlStateNormal];
// 設置選中狀態(tài)下圖片
imgR = CGImageCreateWithImageInRect(selBigImage.CGImage, clipR);
image = [UIImage imageWithCGImage:imgR];
// 設置按鈕的圖片
[btn setImage:image forState:UIControlStateSelected];
// 設置選中背景圖片
[btn setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected];
// 監(jiān)聽按鈕的點擊
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
// 默認選中第一個
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.搞個定時器肠骆,每隔一段時間就旋轉(zhuǎn)一定的角度,1秒旋轉(zhuǎn)45°
#pragma mark - 暫停旋轉(zhuǎn)
- (void)pause
{
self.link.paused = YES;
}
#pragma mark - 每隔一段時間旋轉(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 - 點擊開始選號的時候
- (IBAction)startPicker:(id)sender {
// 不需要定時器旋轉(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];
// 點擊哪個星座,就把當前星座指向中心點上面
// 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)盤的實現(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];
}
}
// 設置UIImageView的尺寸
// contentRect:按鈕的尺寸
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
// 計算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