*****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. JavaOne_2016演讲视频:

    http://list.youku.com/albumlist/show?id=28553407&qq-pf-to=pcqq.group

  2. 树形DP水题杂记

    BZOJ1131: [POI2008]Sta 题意:给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大. 题解:记录每个点的深度,再根据根节点的深度和逐层推导出其他点的深度和. ...

  3. node.js 调用天气webservice接口

    首先安装soap模块 npm install soap 1 2 3 4 5 6 7 8 9 10   var soap = require('soap');   var url = 'http://w ...

  4. 使用javamail发信过程中的一些问题及解决方法

    http://www.blogjava.net/TrampEagle/archive/2006/05/26/48326.html 今天在研究javamail发信的过程中,出现了一些小问题,现总结如下, ...

  5. ACM: HDU 5418 Victor and World - Floyd算法+dp状态压缩

    HDU 5418 Victor and World Time Limit:2000MS     Memory Limit:131072KB     64bit IO Format:%I64d & ...

  6. nohup命令浅析

    要将一个命令放到后台执行,我们一般使用nohup sh command & &都知道是放到后台执行这个命令,那么nohup是做什么的? 这就要从unix的信号说起,unix的信号机制可 ...

  7. usaco silver刷水~其实是回顾一下,补题解

    [BZOJ1606][Usaco2008 Dec]Hay For Sale 裸01背包 ;i<=n;i++) for(int j=m;j>=a[i];j--) f[j]=max(f[j], ...

  8. Jquery_jquery中attr和prop的区别

    在高版本的jquery引入prop方法后,什么时候该用prop?什么时候用attr?它们两个之间有什么区别?这些问题就出现了. 关于它们两个的区别,网上的答案很多.这里谈谈我的心得,我的心得很简单: ...

  9. LBS基站数据解析接口

    http://www.cellocation.com/interfac/#hybrid http://www.cellid.cn/ https://www.juhe.cn/docs/api/id/8

  10. docker 报错Failed to start Docker Storage Setup. 的处理基本都是容器满了

    :: localhost docker-storage-setup: Volume group extents): required. Apr :: localhost systemd: docker ...