*****HMViewController.m

  1. #import "HMViewController.h"
  2.  
  3. #import "HMWheelView.h"
  4.  
  5. @interface HMViewController ()
  6.  
  7. @property (nonatomic, weak) HMWheelView *wheelView;
  8.  
  9. @end
  10.  
  11. @implementation HMViewController
  12. - (IBAction)start:(id)sender {
  13. [_wheelView startRotating];
  14. }
  15. - (IBAction)stop:(id)sender {
  16. [_wheelView stopRotating];
  17. }
  18.  
  19. - (void)viewDidLoad
  20. {
  21. [super viewDidLoad];
  22. // Do any additional setup after loading the view, typically from a nib.
  23. HMWheelView *wheel = [HMWheelView wheelView];
  24.  
  25. wheel.center = self.view.center;
  26.  
  27. [self.view addSubview:wheel];
  28.  
  29. _wheelView = wheel;
  30.  
  31. }
  32.  
  33. - (void)didReceiveMemoryWarning
  34. {
  35. [super didReceiveMemoryWarning];
  36. // Dispose of any resources that can be recreated.
  37. }
  38.  
  39. @end

***HMWheelView.m

  1. #import "HMWheelView.h"
  2.  
  3. #import "HMWheelButton.h"
  4.  
  5. #define angle2radian(x) ((x) / 180.0 * M_PI)
  6.  
  7. @interface HMWheelView ()
  8. @property (weak, nonatomic) IBOutlet UIImageView *rotationView;
  9.  
  10. @property (nonatomic, weak) UIButton *selectedButton;
  11.  
  12. @property (nonatomic, strong) CADisplayLink *link;
  13.  
  14. @end
  15.  
  16. @implementation HMWheelView
  17.  
  18. + (instancetype)wheelView
  19. {
  20. return [[NSBundle mainBundle] loadNibNamed:@"HMWheelView" owner:nil options:nil][];
  21. }
  22.  
  23. // 还有没连号线
  24. - (id)initWithCoder:(NSCoder *)aDecoder
  25. {
  26. if (self = [super initWithCoder:aDecoder]) {
  27.  
  28. NSLog(@"initWithCoder----%@",_rotationView);
  29.  
  30. }
  31. return self;
  32. }
  33.  
  34. // 连好线
  35. #warning 添加按钮
  36. - (void)awakeFromNib
  37. {
  38.  
  39. _rotationView.userInteractionEnabled = YES;
  40.  
  41. // 裁剪的大图片
  42. UIImage *bigImage = [UIImage imageNamed:@"LuckyAstrology"];
  43. UIImage *selectedImage = [UIImage imageNamed:@"LuckyAstrologyPressed"];
  44.  
  45. // 图片的尺寸
  46. CGFloat imageW = * [UIScreen mainScreen].scale;
  47. CGFloat imageH = * [UIScreen mainScreen].scale;
  48.  
  49. for (int i = ; i < ; i++) {
  50. // 创建按钮
  51. HMWheelButton *button = [HMWheelButton buttonWithType:UIButtonTypeCustom];
  52.  
  53. // 锚点
  54. button.layer.anchorPoint = CGPointMake(0.5, );
  55. // 位置
  56. button.layer.position = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5);
  57.  
  58. // 旋转按钮
  59. button.layer.transform = CATransform3DMakeRotation(angle2radian(i * ), , , );
  60.  
  61. // 尺寸
  62. button.bounds = CGRectMake(, , , );
  63.  
  64. // 设置选中时候的背景图片
  65. [button setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected];
  66.  
  67. // 设置按钮的图片
  68. // image:裁剪的图片
  69. // rect:裁剪的尺寸
  70. CGRect clipRect = CGRectMake(i * imageW, , imageW, imageH);
  71. CGImageRef smallImage = CGImageCreateWithImageInRect(bigImage.CGImage, clipRect);
  72. [button setImage:[UIImage imageWithCGImage:smallImage] forState:UIControlStateNormal];
  73.  
  74. // 设置选中的图片
  75. CGImageRef selectedSmallImage = CGImageCreateWithImageInRect(selectedImage.CGImage, clipRect);
  76. [button setImage:[UIImage imageWithCGImage:selectedSmallImage] forState:UIControlStateSelected];
  77.  
  78. // 监听点击事件
  79. [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchDown];
  80.  
  81. if (i == ) {
  82. [self btnClick:button];
  83. }
  84.  
  85. [_rotationView addSubview:button];
  86.  
  87. }
  88. }
  89.  
  90. #warning 监听按钮点击
  91. - (void)btnClick:(UIButton *)button
  92. {
  93. _selectedButton.selected = NO;
  94. button.selected = YES;
  95. _selectedButton = button;
  96. }
  97.  
  98. #warning 开始旋转
  99. - (void)startRotating
  100. {
  101. self.link.paused = NO;
  102.  
  103. }
  104.  
  105. - (void)stopRotating
  106. {
  107. _link.paused = YES;
  108. }
  109.  
  110. - (CADisplayLink *)link
  111. {
  112.  
  113. if (_link == nil) {
  114. CADisplayLink *link = [CADisplayLink displayLinkWithTarget:self selector:@selector(update)];
  115.  
  116. [link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
  117. _link = link;
  118. }
  119. return _link;
  120. }
  121. // 60 45 / 60.0
  122. - (void)update
  123. {
  124. _rotationView.transform = CGAffineTransformRotate(_rotationView.transform, angle2radian( / 60.0));
  125. }
  126.  
  127. - (IBAction)start:(id)sender {
  128.  
  129. // 1.不要和用户交互
  130. _rotationView.userInteractionEnabled = NO;
  131.  
  132. // 2.取消慢慢的旋转
  133. [self stopRotating];
  134.  
  135. CABasicAnimation *anim = [CABasicAnimation animation];
  136.  
  137. anim.keyPath = @"transform.rotation";
  138.  
  139. anim.toValue = @(M_PI * * );
  140.  
  141. anim.duration = 0.5;
  142.  
  143. anim.delegate = self;
  144.  
  145. [_rotationView.layer addAnimation:anim forKey:nil];
  146.  
  147. }
  148.  
  149. - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
  150. {
  151. _rotationView.userInteractionEnabled = YES;
  152.  
  153. // 让选中按钮回到最在上面的中间位置:
  154. CGFloat angle = atan2(_selectedButton.transform.b, _selectedButton.transform.a);
  155.  
  156. NSLog(@"%f",angle);
  157.  
  158. // 把我们的转盘反向旋转这么多°
  159. _rotationView.transform = CGAffineTransformMakeRotation(-angle);
  160.  
  161. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  162. [self startRotating];
  163. });
  164. }
  165.  
  166. @end

***HMWheelView.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface HMWheelView : UIView
  4.  
  5. + (instancetype)wheelView;
  6.  
  7. // 开始旋转
  8. - (void)startRotating;
  9.  
  10. // 停止旋转
  11. - (void)stopRotating;
  12.  
  13. @end

****HMWheelButton.m

  1. #import "HMWheelButton.h"
  2.  
  3. @implementation HMWheelButton
  4. //转盘 按钮
  5. - (CGRect)imageRectForContentRect:(CGRect)contentRect
  6. {
  7. CGFloat imageW = ;
  8. CGFloat imageH = ;
  9. CGFloat imageX = (contentRect.size.width - imageW) * 0.5;
  10. CGFloat imageY = ;
  11.  
  12. return CGRectMake(imageX, imageY, imageW, imageH);
  13. }
  14. //去除高亮
  15. - (void)setHighlighted:(BOOL)highlighted
  16. {
  17.  
  18. }
  19.  
  20. @end

****HMWheelButton.h

  1. #import <UIKit/UIKit.h>
  2.  
  3. @interface HMWheelButton : UIButton
  4.  
  5. @end

IOS第18天(10,核心动画-转盘,自定义buton,旋转动画)的更多相关文章

  1. [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)

    http://blog.csdn.net/yanzi1225627/article/details/22439119 众所周知,想要让ImageView旋转的话,可以用setRotation()让其围 ...

  2. Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡...)

    众所周知,想要让ImageView旋转的话,可以用setRotation()让其围绕中心点旋转,但这个旋转是不带动画的,也就是旋转屏幕时图片噌的一下就转过去了,看不到旋转的过程,此UI体验不大好,为此 ...

  3. iOS 帧动画之翻转和旋转动画

    记录两个比较简单的动画,一个是翻转的动画,一个是旋转的动画. 旋转动画: 1 [UIView animateWithDuration:3 animations:^{ if (formView) { f ...

  4. IOS第18天(9,核心动画-动画组)

    ****动画组 // 核心动画都是假象,不能改变layer的真实属性的值// 展示的位置和实际的位置不同.实际位置永远在最开始位置 #import "HMViewController.h&q ...

  5. IOS第18天(1,核心动画layer, 旋转,缩放,平移,边框,剪裁,圆角)

    ****动画效果 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [UIView animateWithDurat ...

  6. IOS第18天(8,核心动画转场动画)

    ***翻页效果 #import "HMViewController.h" @interface HMViewController () @property (weak, nonat ...

  7. IOS第18天(4,核心动画,时钟效果,定时器,图片旋转角度,CALayer 锚点,获取当前,小时,秒,分)

    **** #import "HMViewController.h" // 每秒秒针转6度 #define perSecendA 6 // 每分钟分针转6度 #define perM ...

  8. Android动画主要包含补间动画(Tween)View Animation、帧动画(Frame)Drawable Animation、以及属性动画Property Animation

    程序运行效果图: Android动画主要包含补间动画(Tween)View Animation.帧动画(Frame)Drawable Animation.以及属性动画Property Animatio ...

  9. UIView动画效果之----翻转.旋转.偏移.翻页.缩放.取反的动画效

    翻转的动画 //开始动画 [UIView beginAnimations:@"doflip" context:nil]; //设置时常 [UIView setAnimationDu ...

随机推荐

  1. Python与Hack之window下运行带参数的Python脚本,实现一个简单的端口扫描器

    1.前提是:windows已经配置好Python的环境变量: 2.进入cmd命令行模式: **输入python命令,检测是否环境配置好:显示这样说明配置环境变量没问题 **用cd命令进入Python脚 ...

  2. 09_IO流

    1. IO(Input Output)流 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作数据分为两种: 字节流和字符流 流按类型分 ...

  3. 操作TAB文件和TStringGrid赋值;

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  4. thymeleaf 基本表达式

    Thymeleaf 基本表达式 如需了解thymeleaf以及thymeleaf整合spring,请参考<Thymeleaf模板引擎使用>.<Thymeleaf 集成spring&g ...

  5. iOS 为类添加Xib里面配置的view

    创建Empty文件,最好与其Controller同名, 在File's Owner的类属性里面指明其所属类(或者说它是个什么Controller), 从File's Owner右键拖向内部创建的视图( ...

  6. 接口测试第二课(Fiddler实现APP抓包)

    Fiddler简介: Fiddler是强大且好用的Web调试工具之一,它能记录客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数 据. Fiddler 的运行机制其实就 ...

  7. uva748 - Exponentiation

    import java.io.*; import java.text.*; import java.util.*; import java.math.*; public class Exponenti ...

  8. 【原】iOS学习18之OC内存管理高级

    1.属性的内存管理 1> 属性的语义特性 2> assign下的属性内部实现 @property (nonatomic, assign) NSString *name; @synthesi ...

  9. iOS之05-三大特性之封装

    本次主要学习面向对象的三大特性:封装.继承和多态中的封装 封装 1. 好处 降低耦合率 可重复调用类中的属性 提高安全性,外部不能随便修改变量的值,保证了数据的安全性 2. set方法 1.作用:提供 ...

  10. 2016年AR行业十大热点事件汇总

    2016年即将接近尾声,增强现实在今年完成了里程碑式的跃进.无论是从新玩法的开发还是从大众接受度,以及行业巨头的青睐,无不证明这AR的无线潜力,故而,2016年算是AR的崛起之年. 纵观全年AR新闻事 ...