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 ...
随机推荐
- Python与Hack之window下运行带参数的Python脚本,实现一个简单的端口扫描器
1.前提是:windows已经配置好Python的环境变量: 2.进入cmd命令行模式: **输入python命令,检测是否环境配置好:显示这样说明配置环境变量没问题 **用cd命令进入Python脚 ...
- 09_IO流
1. IO(Input Output)流 IO流用来处理设备之间的数据传输 Java对数据的操作时通过流的方式 Java用于操作流的对象都在IO包中 流按操作数据分为两种: 字节流和字符流 流按类型分 ...
- 操作TAB文件和TStringGrid赋值;
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- thymeleaf 基本表达式
Thymeleaf 基本表达式 如需了解thymeleaf以及thymeleaf整合spring,请参考<Thymeleaf模板引擎使用>.<Thymeleaf 集成spring&g ...
- iOS 为类添加Xib里面配置的view
创建Empty文件,最好与其Controller同名, 在File's Owner的类属性里面指明其所属类(或者说它是个什么Controller), 从File's Owner右键拖向内部创建的视图( ...
- 接口测试第二课(Fiddler实现APP抓包)
Fiddler简介: Fiddler是强大且好用的Web调试工具之一,它能记录客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数 据. Fiddler 的运行机制其实就 ...
- uva748 - Exponentiation
import java.io.*; import java.text.*; import java.util.*; import java.math.*; public class Exponenti ...
- 【原】iOS学习18之OC内存管理高级
1.属性的内存管理 1> 属性的语义特性 2> assign下的属性内部实现 @property (nonatomic, assign) NSString *name; @synthesi ...
- iOS之05-三大特性之封装
本次主要学习面向对象的三大特性:封装.继承和多态中的封装 封装 1. 好处 降低耦合率 可重复调用类中的属性 提高安全性,外部不能随便修改变量的值,保证了数据的安全性 2. set方法 1.作用:提供 ...
- 2016年AR行业十大热点事件汇总
2016年即将接近尾声,增强现实在今年完成了里程碑式的跃进.无论是从新玩法的开发还是从大众接受度,以及行业巨头的青睐,无不证明这AR的无线潜力,故而,2016年算是AR的崛起之年. 纵观全年AR新闻事 ...