IOS第18天(4,核心动画,时钟效果,定时器,图片旋转角度,CALayer 锚点,获取当前,小时,秒,分)

****

#import "HMViewController.h"

// 每秒秒针转6度
#define perSecendA 6

// 每分钟分针转6度
#define perMinuteA 6

// 每小时时针转6度
#define perHourA 30

// 每分钟时针转6度
#define perMinuteHourA 0.5

#define angle2radian(x) ((x) / 180.0 * M_PI)

@interface HMViewController ()
{
    CALayer *_second;
    CALayer *_minute;
    CALayer *_hour;
}
@property (weak, nonatomic) IBOutlet UIView *redView;
@property (weak, nonatomic) IBOutlet UIImageView *clockView;

@end

@implementation HMViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 1.添加秒针
    [self addSecond];

    // 2.添加分针
    [self addMintue];

    // 3.添加时针
    [self addHour];

    // 创建定时器
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(update) userInfo:nil repeats:YES];

    [self update];
}

- (void)addHour
{
    CGFloat imageW = _clockView.bounds.size.width;
    CGFloat imageH = _clockView.bounds.size.height;

    // 1.添加时针
    CALayer *hour = [CALayer layer];

    // 2.设置锚点
    hour.anchorPoint = CGPointMake(0.5, 1);

    // 3.设置位置
    hour.position = CGPointMake(imageW * 0.5, imageH * 0.5);

    // 4.设置尺寸
    hour.bounds = CGRectMake(0, 0, 4, imageH * 0.5 - 50);

    // 5.红色
    hour.backgroundColor = [UIColor blackColor].CGColor;

    hour.cornerRadius = 4;

    // 添加到图层上
    [_clockView.layer addSublayer:hour];

    _hour = hour;
}

// 添加分针
- (void)addMintue
{
    CGFloat imageW = _clockView.bounds.size.width;
    CGFloat imageH = _clockView.bounds.size.height;

    // 1.添加分针
    CALayer *minute = [CALayer layer];

    // 2.设置锚点
    minute.anchorPoint = CGPointMake(0.5, 1);

    // 3.设置位置
    minute.position = CGPointMake(imageW * 0.5, imageH * 0.5);

    // 4.设置尺寸
    minute.bounds = CGRectMake(0, 0, 2, imageH * 0.5 - 30);

    // 5.红色
    minute.backgroundColor = [UIColor blueColor].CGColor;

    // 添加到图层上
    [_clockView.layer addSublayer:minute];

    _minute = minute;
}

// 添加秒针
- (void)addSecond
{
    CGFloat imageW = _clockView.bounds.size.width;
    CGFloat imageH = _clockView.bounds.size.height;

    // 1.添加秒针
    CALayer *second = [CALayer layer];

    // 2.设置锚点
    second.anchorPoint = CGPointMake(0.5, 1);

    // 3.设置位置
    second.position = CGPointMake(imageW * 0.5, imageH * 0.5);

    // 4.设置尺寸
    second.bounds = CGRectMake(0, 0, 1, imageH * 0.5 - 20);

    // 5.红色
    second.backgroundColor = [UIColor redColor].CGColor;

    // 添加到图层上
    [_clockView.layer addSublayer:second];

    _second = second;
}

- (void)update
{
    // 获取秒数
    // 获取日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // 获取日期组件
    NSDateComponents *compoents = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];

    // 获取秒数
    CGFloat sec = compoents.second;

    // 获取分钟
    CGFloat minute = compoents.minute;

    // 获取小时
    CGFloat hour = compoents.hour;

    // 算出秒针旋转多少°
    CGFloat secondA = sec * perSecendA;

    // 算出分针旋转多少°
    CGFloat minuteA = minute * perMinuteA;

    // 算出时针旋转多少°
    CGFloat hourA = hour * perHourA;
    hourA += minute * perMinuteHourA;

    // 旋转秒针
    _second.transform = CATransform3DMakeRotation(angle2radian(secondA), 0, 0, 1);

    // 旋转分针
    _minute.transform = CATransform3DMakeRotation(angle2radian(minuteA), 0, 0, 1);

    // 旋转时针
    _hour.transform = CATransform3DMakeRotation(angle2radian(hourA), 0, 0, 1);

}
@end
时间: 2024-08-12 02:28:28

IOS第18天(4,核心动画,时钟效果,定时器,图片旋转角度,CALayer 锚点,获取当前,小时,秒,分)的相关文章

IOS第18天(9,核心动画-动画组)

****动画组 // 核心动画都是假象,不能改变layer的真实属性的值// 展示的位置和实际的位置不同.实际位置永远在最开始位置 #import "HMViewController.h" @interface HMViewController () @property (weak, nonatomic) IBOutlet UIView *redView; @end @implementation HMViewController - (void)viewDidLoad { [supe

IOS第18天(1,核心动画layer, 旋转,缩放,平移,边框,剪裁,圆角)

****动画效果 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [UIView animateWithDuration:1 animations:^{ // 旋转 // _imageView.layer.transform = CATransform3DMakeRotation(M_PI, 1, 1, 0); // 平移 // _imageView.layer.transform = CATransform3

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 { [_whee

IOS第18天(8,核心动画转场动画)

***翻页效果 #import "HMViewController.h" @interface HMViewController () @property (weak, nonatomic) IBOutlet UIImageView *imageView; @property (nonatomic, assign) int index; @end @implementation HMViewController - (void)viewDidLoad { [super viewDidL

ios开发-UI进阶-核心动画-时钟动画小案例

[注意]转载时请注明出处博客园-吃唐僧肉的小悟空http://www.cnblogs.com/hukezhu/ 今天使用CALayer的"定位点(锚点)"实现了一个时钟动画,其实就是一个小的时钟,只是实现了功能,没有做出绚丽的效果.使用UIView实现的,其实只是单纯的使用layer也可以实现.主要用到了 Quartz2D画图\ 事件处理\核心动画方面的知识. 代码不是很多,直接附上源码,注释比较详细,在源码后面再进行解释其中的一些知识点和注意点. 下图为应用截图,使用gif,没有制作

iOS UI进阶-3.0 核心动画

lCore Animation是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍,使用它需要先添加QuartzCore.framework和引入对应的框架<QuartzCore/QuartzCore.h> Core Animation的使用,请参考最下面的博客. 由于Core Animation动画,改变的只是一个影子,实际的位置和尺寸都不会有变化.因而,在实际开发中,还是建议直接使用UIView动画. UIView动画 - (void)testViewSimp

ios开发之图层与核心动画一:图层CALayer的认识

#import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *redView; @property (weak, nonatomic) IBOutlet UIImageView *imageV; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]

iOS:CALayer核心动画层

CALayer:核心动画层 简介: Core Animation 是跨平台的,支持iOS环境和Mac OS X环境 学习核心动画之前,需要先理解CALayer,因为核心动画操作的对象不是UIView,而是CALayer CALayer是核心动画的基础,可以做圆角.阴影.边框等效果 每个UIView内部都有一个Layer的属性 在实现核心动画时,本质上是将CALayer中的内容转换成位图,从而便于图形硬件的操纵 UIView的CALayer基本演练: 演练设置UIView中的CALayer属性 –

IOS学习--核心动画

1.CoreAnimation的介绍 Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代码就可以实现非常强大的功能. Core Animation可以用在Mac OS X和iOS平台. Core Animation的动画执行过程都是在后台操作的,不会阻塞主线程. 要注意的是,Core Animation是直接作用在CALayer上的,并非UIView 详细地址:http://www.cnb