IOS第18天(10,核心動畫-轉盤,自訂buton,旋轉動畫)

來源:互聯網
上載者:User

標籤:

*****HMViewController.m

#import "HMViewController.h"#import "HMWheelView.h"@interface HMViewController ()@property (nonatomic, weak) HMWheelView *wheelView;@end@implementation HMViewController- (IBAction)start:(id)sender {    [_wheelView startRotating];}- (IBAction)stop:(id)sender {    [_wheelView stopRotating];}- (void)viewDidLoad{    [super viewDidLoad];    // Do any additional setup after loading the view, typically from a nib.    HMWheelView *wheel = [HMWheelView wheelView];        wheel.center = self.view.center;        [self.view addSubview:wheel];        _wheelView = wheel;    }- (void)didReceiveMemoryWarning{    [super didReceiveMemoryWarning];    // Dispose of any resources that can be recreated.}@end

***HMWheelView.m

#import "HMWheelView.h"#import "HMWheelButton.h"#define angle2radian(x) ((x) / 180.0 * M_PI)@interface HMWheelView ()@property (weak, nonatomic) IBOutlet UIImageView *rotationView;@property (nonatomic, weak) UIButton *selectedButton;@property (nonatomic, strong) CADisplayLink *link;@end@implementation HMWheelView+ (instancetype)wheelView{    return [[NSBundle mainBundle] loadNibNamed:@"HMWheelView" owner:nil options:nil][0];}// 還有沒連號線- (id)initWithCoder:(NSCoder *)aDecoder{    if (self = [super initWithCoder:aDecoder]) {                NSLog(@"initWithCoder----%@",_rotationView);            }    return self;}// 連好線#warning 添加按鈕- (void)awakeFromNib{        _rotationView.userInteractionEnabled = YES;            // 裁剪的大圖片    UIImage *bigImage = [UIImage imageNamed:@"LuckyAstrology"];    UIImage *selectedImage = [UIImage imageNamed:@"LuckyAstrologyPressed"];        // 圖片的尺寸    CGFloat imageW = 40 * [UIScreen mainScreen].scale;    CGFloat imageH = 47 * [UIScreen mainScreen].scale;        for (int i = 0; i < 12; i++) {        // 建立按鈕        HMWheelButton *button = [HMWheelButton buttonWithType:UIButtonTypeCustom];                        // 錨點        button.layer.anchorPoint = CGPointMake(0.5, 1);        // 位置        button.layer.position = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);                // 旋轉按鈕        button.layer.transform = CATransform3DMakeRotation(angle2radian(i * 30), 0, 0, 1);                // 尺寸        button.bounds = CGRectMake(0, 0, 68, 143);                        // 設定選中時候的背景圖片        [button setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected];                // 設定按鈕的圖片        // image:裁剪的圖片        // rect:裁剪的尺寸       CGRect clipRect = CGRectMake(i * imageW, 0, imageW, imageH);       CGImageRef smallImage = CGImageCreateWithImageInRect(bigImage.CGImage, clipRect);        [button setImage:[UIImage imageWithCGImage:smallImage] forState:UIControlStateNormal];                // 設定選中的圖片        CGImageRef selectedSmallImage = CGImageCreateWithImageInRect(selectedImage.CGImage, clipRect);        [button setImage:[UIImage imageWithCGImage:selectedSmallImage] forState:UIControlStateSelected];                // 監聽點擊事件        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];                if (i == 0) {            [self btnClick:button];        }                        [_rotationView addSubview:button];                    }}#warning 監聽按鈕點擊- (void)btnClick:(UIButton *)button{    _selectedButton.selected = NO;    button.selected = YES;    _selectedButton = button;}#warning 開始旋轉- (void)startRotating{    self.link.paused = NO;}- (void)stopRotating{    _link.paused = YES;}- (CADisplayLink *)link{    if (_link == nil) {        CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];                [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];        _link = link;    }    return _link;}// 60  45 / 60.0- (void)update{    _rotationView.transform = CGAffineTransformRotate(_rotationView.transform, angle2radian(45 / 60.0));}- (IBAction)start:(id)sender {        // 1.不要和使用者互動    _rotationView.userInteractionEnabled = NO;        // 2.取消慢慢的旋轉    [self stopRotating];        CABasicAnimation *anim = [CABasicAnimation animation];    anim.keyPath = @"transform.rotation";    anim.toValue = @(M_PI * 2 * 3);    anim.duration = 0.5;        anim.delegate = self;    [_rotationView.layer addAnimation:anim forKey:nil];    }- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{    _rotationView.userInteractionEnabled = YES;            // 讓選中按鈕回到最在上面的中間位置:    CGFloat angle = atan2(_selectedButton.transform.b, _selectedButton.transform.a);        NSLog(@"%f",angle);        // 把我們的轉盤反向旋轉這麼多°    _rotationView.transform = CGAffineTransformMakeRotation(-angle);        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{        [self startRotating];    });}@end

***HMWheelView.h

#import <UIKit/UIKit.h>@interface HMWheelView : UIView+ (instancetype)wheelView;// 開始旋轉- (void)startRotating;// 停止旋轉- (void)stopRotating;@end


****HMWheelButton.m

#import "HMWheelButton.h"@implementation HMWheelButton //轉盤  按鈕- (CGRect)imageRectForContentRect:(CGRect)contentRect{    CGFloat imageW = 40;    CGFloat imageH = 47;    CGFloat imageX = (contentRect.size.width - imageW) * 0.5;    CGFloat imageY = 20;        return CGRectMake(imageX, imageY, imageW, imageH);}//去除高亮- (void)setHighlighted:(BOOL)highlighted{}@end


****HMWheelButton.h

#import <UIKit/UIKit.h>@interface HMWheelButton : UIButton@end

 

IOS第18天(10,核心動畫-轉盤,自訂buton,旋轉動畫)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.