*****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][];
} // 还有没连号线
- (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 = * [UIScreen mainScreen].scale;
CGFloat imageH = * [UIScreen mainScreen].scale; for (int i = ; i < ; i++) {
// 创建按钮
HMWheelButton *button = [HMWheelButton buttonWithType:UIButtonTypeCustom]; // 锚点
button.layer.anchorPoint = CGPointMake(0.5, );
// 位置
button.layer.position = CGPointMake(self.bounds.size.width * 0.5, self.bounds.size.height * 0.5); // 旋转按钮
button.layer.transform = CATransform3DMakeRotation(angle2radian(i * ), , , ); // 尺寸
button.bounds = CGRectMake(, , , ); // 设置选中时候的背景图片
[button setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected]; // 设置按钮的图片
// image:裁剪的图片
// rect:裁剪的尺寸
CGRect clipRect = CGRectMake(i * imageW, , 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 == ) {
[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( / 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 * * ); 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)( * 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 = ;
CGFloat imageH = ;
CGFloat imageX = (contentRect.size.width - imageW) * 0.5;
CGFloat imageY = ; 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,旋转动画)的更多相关文章

  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. express-19 路由2

    组织路由 在主应用程序文件中定义所有路由太笨重了.那样不仅会导致那个文件一直增长,还不利于功能的分离,因为那个文件里已经有很多东西了. 四条组织路由的指导原则 给路由处理器用命名函数: 到目前为止,我 ...

  2. ubuntu 下python版本切换

    1. 安装ubuuntu 14.04之后python的默认版本为2.7.6但是我想使用python的版本为3.4 可以打开终端:输入:alias python = python3

  3. 如何定义移动端字体Font-Family?

    1.对于IOS 手机系统,默认中文字体是Heiti SC.默认英文字体是Helvetica.默认数字字体是HelveticaNeue.无微软雅黑字体: 2.对于Android 手机系统,默认中文字体是 ...

  4. 去除手机端触摸滑动事件ontouchmove

    window.ontouchmove=function(e){ e.preventDefault && e.preventDefault(); e.returnValue=false; ...

  5. 【面经】用递归方法对二叉树进行层次遍历 && 二叉树深度

    void PrintNodeAtLevel(BiTree T,int level) { // 空树或层级不合理 ) return; == level) { cout << T->da ...

  6. LaunchCharacter

    /** Set a pending launch velocity on the Character. This velocity will be processed on the next Char ...

  7. nginx负载均衡之基于客户端cookie的会话保持

    通过ip_hash做会话保持有一定的缺陷,这个是通过客户端ip来实现.同一个网络下众多客户端访问服务器会被扔到同一台机器,再或者是CDN也 会导致负载不均衡.所以要实现通过客户端cookie实现,包括 ...

  8. 为OpenResty增加nginx_upload_module模块

    1.下载 http://www.grid.net.ru/nginx/download/nginx_upload_module-2.2.0.tar.gz 或 http://pan.baidu.com/s ...

  9. 没人告诉你关于z-index的一些事

    关于z-index的问题是很多程序员都不知道它是如何起作用的.说起来不难,但是大部分人并没有花时间去看规范,这往往会照成严重的后果. 你不信?那就一起来看看下面的问题. 问题 在下面的HTML我们写了 ...

  10. LINUX 下通过lsof恢复被误删除的文件

    当Linux计算机受到入侵时,常见的情况是日志文件被删除,以掩盖攻击者的踪迹.管理错误也可能导致意外删除重要的文件,比如在清理旧日志时,意外地删除了数据库的活动事务日志.有时可以通过lsof来恢复这些 ...