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 ...
随机推荐
- no-jquery 05 Utilities
Utilities type // is this a function? typeof someValue === 'function'; // is this an object? someVal ...
- 【虚拟机】苹果虚拟机mac10.11.6+Xcode8.1
[虚拟机]苹果虚拟机mac10.11.6+Xcode8.1本虚拟机加装Xcode8.1,方便大家更好学习Swift3.0语言以及iOS开发.安装注意事项:第一步:确认硬件:1.确认主板以及cpu支持虚 ...
- iOS instancetype or id ?
The id type simply says a method will return a reference to an object. It could be any object of any ...
- Git常用命令举例
clone一个git project到本地 git clone https://github.com/huahuiyang/network-certification.git 到这个目录下,可以发现有 ...
- Shell 编程基础之基本语法结构汇总
一.条件语句 简单条件 if [ condition ]; then # 当 condition 成立时,执行内容: fi # 将 if 反过来写,fi 结束 if 之意 复杂条件 if [ cond ...
- js库写法
前言: 现在javascript库特别多,其写法各式各样,总结几种我们经常见到的,作为自己知识的积累.而目前版本的 JavaScript 并未提供一种原生的.语言级别的模块化组织模式,而是将模块化的方 ...
- EF框架step by step(7)—Code First DataAnnotations(1)
Data annotation特性是在.NET 3.5中引进的,给ASP.NET web应用中的类提供了一种添加验证的方式.Code First允许你使用代码来建立实体框架模型,同时允许用Data a ...
- 80端口被占用,pid=4强制杀进程杀不掉
解决: http://www.cnblogs.com/myjavawork/articles/1867839.html 把SqlServer的该服务关闭.虽然他显示的进程号跟搜出来的不一样.
- word多级编号,如何让第一级为大写“一”,其他级别均为小写1.
自定义里面设置了第一级为大写,2级.3级首字跟着变为大写,是因为2.3级没有勾选"正规形式编号",如图:
- UVA 11754 (暴力+中国剩余定理)
题目链接: http://www.bnuoj.com/v3/problem_show.php?pid=20172 题目大意:有C个模方程,每个方程可能有k余数,求最小的S个解. 解题思路: 看见模方程 ...