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 ...
随机推荐
- HTML5 重要标签以及属性学习
1.一个标签可以有多个,class=“A B C ” 效果: 2.padding的扩展:当padding的值是正的时候,元素显示的大小会变大:当padding的值是负的时候,元素显示的大小会变小 pa ...
- 学习 Message(5): 关于 TApplicationEvents.OnMessage 的第二个参数 可以屏蔽 TWebBrowser右键菜单:
http://www.cnblogs.com/del/archive/2008/10/25/1319318.html TApplicationEvents.OnMessage 的第二个参数 Handl ...
- AC自动机(二维) UVA 11019 Matrix Matcher
题目传送门 题意:训练指南P218 分析:一行一行的插入,一行一行的匹配,当匹配成功时将对应子矩阵的左上角位置cnt[r][c]++;然后统计 cnt[r][c] == x 的数量 #include ...
- apache activemq 学习笔记
0.activemq的概念 activemq实现了jms(java Message server),用于接收,发送,处理消息的开源消息总线. 1.activemq和jms的区别 jms说白了就是jav ...
- tidyr包--数据处理包
tidyr包的作者是Hadley Wickham.这个包常跟dplyr结合使用.本文将介绍tidyr包中下述四个函数的用法: gather—宽数据转为长数据.类似于reshape2包中的melt函数 ...
- 使用CSS代码修改博客模板
在修改设置使公告栏里的头像更新为新的头像时发现里边还有“页面定制CSS代码”这一选项,查了一下发现这东西可以对页面做一些个性化的调整.正好目前我使用的这个模板标题和导航栏的字体实在难看,顺手修改了一下 ...
- BZOJ1841 : 蚂蚁搬家
树分治,对于每个分治结构,维护两棵线段树. 第一棵按dfs序维护所有点到重心的距离,第二棵维护每个分支的最长链. 那么当前结构对答案的贡献就是第二棵线段树的最大值$+$次大值. 对于操作$0$,如果是 ...
- BZOJ3072 : [Pa2012]Two Cakes
考虑DP,设$f[i][j]$表示考虑了$a[1..i]$和$b[1..j]$的最小代价. 若$a[i]==b[j]$,则$f[i][j]=\min(f[i-1][j],f[i][j-1])+1$. ...
- BZOJ 3223 & 区间翻转
题意: 就是贴个代码,这是我入门题的弱化版..然而一共还是写了40分钟,不专注(一边看比赛一边打)是一个问题,splay每个操作的细节确实有点多(什么时候updata啊..什么时候pushdown啊. ...
- 洛谷 P1330 封锁阳光大学 Label:染色问题
题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校园是一张由N个点构成的无向图,N个点之间由M ...