IOS第18天(10,核心动画-转盘,自定义buton,旋转动画)
*****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,旋转动画)的更多相关文章
- [转]Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡出效果)
http://blog.csdn.net/yanzi1225627/article/details/22439119 众所周知,想要让ImageView旋转的话,可以用setRotation()让其围 ...
- Android UI:看看Google官方自定义带旋转动画的ImageView-----RotateImageView怎么写(附 图片淡入淡...)
众所周知,想要让ImageView旋转的话,可以用setRotation()让其围绕中心点旋转,但这个旋转是不带动画的,也就是旋转屏幕时图片噌的一下就转过去了,看不到旋转的过程,此UI体验不大好,为此 ...
- iOS 帧动画之翻转和旋转动画
记录两个比较简单的动画,一个是翻转的动画,一个是旋转的动画. 旋转动画: 1 [UIView animateWithDuration:3 animations:^{ if (formView) { f ...
- IOS第18天(9,核心动画-动画组)
****动画组 // 核心动画都是假象,不能改变layer的真实属性的值// 展示的位置和实际的位置不同.实际位置永远在最开始位置 #import "HMViewController.h&q ...
- IOS第18天(1,核心动画layer, 旋转,缩放,平移,边框,剪裁,圆角)
****动画效果 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [UIView animateWithDurat ...
- IOS第18天(8,核心动画转场动画)
***翻页效果 #import "HMViewController.h" @interface HMViewController () @property (weak, nonat ...
- IOS第18天(4,核心动画,时钟效果,定时器,图片旋转角度,CALayer 锚点,获取当前,小时,秒,分)
**** #import "HMViewController.h" // 每秒秒针转6度 #define perSecendA 6 // 每分钟分针转6度 #define perM ...
- Android动画主要包含补间动画(Tween)View Animation、帧动画(Frame)Drawable Animation、以及属性动画Property Animation
程序运行效果图: Android动画主要包含补间动画(Tween)View Animation.帧动画(Frame)Drawable Animation.以及属性动画Property Animatio ...
- UIView动画效果之----翻转.旋转.偏移.翻页.缩放.取反的动画效
翻转的动画 //开始动画 [UIView beginAnimations:@"doflip" context:nil]; //设置时常 [UIView setAnimationDu ...
随机推荐
- POJ 2406 KMP/后缀数组
题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就 ...
- no-jquery 02 DOM
DOM Manipulation Creating Elements // IE 5.5+ document.createElement('div'); Inserting Elements Befo ...
- express-8 Handlebars模板引擎(1)
简介 使用JavaScript生成一些HTML document.write('<h1>Please Don\'t Do This</h1>'); document.write ...
- Coursera课程下载和存档计划[转载]
上周三收到Coursera平台的群发邮件,大意是Coursera将在6月30号彻底关闭旧的课程平台,全面升级到新的课程平台上,一些旧的课程资源(课程视频.课程资料)将不再保存,如果你之前学习过相关的课 ...
- VMware 锐捷 NAT模式的服务自动关闭的解决办法
之前一直搞不定VMware和锐捷的问题,校园网,你懂的. 后来发现,使用NAT模式联网时,锐捷会间隔性地终结windows系统的那个关于NAT联网的服务,你可以在计算机管理 - 服务,找到一个写有NA ...
- HIT2739 The Chinese Postman Problem(最小费用最大流)
题目大概说给一张有向图,要从0点出发返回0点且每条边至少都要走过一次,求走的最短路程. 经典的CPP问题,解法就是加边构造出欧拉回路,一个有向图存在欧拉回路的充分必要条件是基图连通且所有点入度等于出度 ...
- js、PHP将分数字符串转换为小数
PHP:$s = "1/3"; $s = str_replace(array('[',']','mod'),array('(',')','%'),$s);//将原三字符串中的只有在 ...
- 来自于2016.2.23的flag
正是中午,百废待兴,写点什么调节一会儿心情吧.正巧有许多的想法. 机房来了许多小朋友,多么像一年之前的我啊,想写题,心又纷乱,但不同的是他们比我强太多了. 停课是什么感觉?停课在机房与寒暑假.双休日在 ...
- CentOS6.4安装mysql2redis
1.安装apr 下载:http://apache.dataguru.cn//apr/apr-1.5.1.tar.gz tar zxvf apr-.tar.gz cd apr- vi configure ...
- ACM Minimum Inversion Number 解题报告 -线段树
C - Minimum Inversion Number Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d &a ...