CALayer的基本使用

CALayer的基本属性在这里不多说了,网上对属性和方法介绍的很清楚,下面直接上demo效果

时钟

这里写图片描述

主要使用到的几个知识点
  • anchorPoint(锚点)与position的使用
  • 用NSCalendar和NSDateComponents拿到日期的组件
#import "ViewController.h"

// 每秒秒针转6°
#define kPerSecondA 6
// 每分钟分针转6°
#define kPerMinuteA 6

// 每小时时针转30°
#define kPerHourA 30

// 每分钟时针转0.5°
#define kPerMinHourA 0.5



@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@property (weak, nonatomic)  CALayer *secLayer;

@property (weak, nonatomic)  CALayer *minLayer;

@property (weak, nonatomic)  CALayer *hourLayer;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 小时
    [self setUpHourLayer];

    // 分
    [self setUpMinLayer];

    // 秒
    [self setUpSecLayer];

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

    // 初始化始终
    [self timeChange];
}

- (void)timeChange
{
    // 获取当前是多少秒
    // 获取当前的日历对象
    NSCalendar *calendar = [NSCalendar currentCalendar];

    // 日期组件:秒,分,小时,年,月,日......
    // NSCalendarUnit:表示日期组件有哪些组成
    NSDateComponents *dataCmp = [calendar components:NSCalendarUnitSecond | NSCalendarUnitMinute | NSCalendarUnitHour fromDate:[NSDate date]];




    // 获取分
    NSInteger minute = dataCmp.minute;

    // 计算下分针转多少度
    CGFloat minA = (minute * kPerMinuteA) / 180.0 * M_PI;

    // 旋转分针
    _minLayer.transform = CATransform3DMakeRotation(minA, 0, 0, 1);



    // 获取小时
    NSInteger hour = dataCmp.hour;

    // 计算时针转多少度 = 当前有多少小时 * 每小时转的度数30° + 当前多少分钟 * 每分钟时针转多少0.5
    CGFloat hourA = (hour * kPerHourA + minute * kPerMinHourA) / 180.0 * M_PI;

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

    // 获取秒数
    NSInteger sec = dataCmp.second;

    // 秒针转多少度
    CGFloat secA = (sec * kPerSecondA) / 180.0 * M_PI;

    // 旋转秒针
    _secLayer.transform = CATransform3DMakeRotation(secA, 0, 0, 1);
}

// 秒
- (void)setUpSecLayer
{
    CALayer *layer = [CALayer layer];

    _secLayer = layer;

    layer.backgroundColor = [UIColor redColor].CGColor;

    // 绕着锚点旋转,设置锚点
    layer.anchorPoint = CGPointMake(0.5, 1);

    layer.position = CGPointMake(_imageView.bounds.size.width * 0.5, _imageView.bounds.size.height * 0.5);

    layer.bounds = CGRectMake(0, 0, 1, 80);

    [_imageView.layer addSublayer:layer];
}

// 分
- (void)setUpMinLayer
{
    CALayer *layer = [CALayer layer];

    _minLayer = layer;

    layer.cornerRadius = 4;

    layer.backgroundColor = [UIColor blackColor].CGColor;

    // 绕着锚点旋转,设置锚点
    layer.anchorPoint = CGPointMake(0.5, 1);

    layer.position = CGPointMake(_imageView.bounds.size.width * 0.5, _imageView.bounds.size.height * 0.5);

    layer.bounds = CGRectMake(0, 0, 4, 80);

    [_imageView.layer addSublayer:layer];
}

// 小时
- (void)setUpHourLayer
{
    CALayer *layer = [CALayer layer];

    _hourLayer = layer;

    layer.cornerRadius = 4;

    layer.backgroundColor = [UIColor blackColor].CGColor;

    // 绕着锚点旋转,设置锚点
    layer.anchorPoint = CGPointMake(0.5, 1);

    layer.position = CGPointMake(_imageView.bounds.size.width * 0.5, _imageView.bounds.size.height * 0.5);

    layer.bounds = CGRectMake(0, 0, 4, 75);

    [_imageView.layer addSublayer:layer];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在iOS上使用Objective-C实现动态壁纸功能,您可以按照以下步骤进行: 1.创建一个新的Xcode项目,并选择Single View Application模板。 2.在Assets.xcassets中创建包含动态壁纸图像的图像集。 3.在AppDelegate.m文件中添加以下代码,以在应用程序启动时注册动态壁纸: ``` - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Register for dynamic wallpapers NSDictionary *wallpaperOptions = @{UIApplicationRegisteredDefaultDynamicWallpaperOptions : @{}}; [[UIApplication sharedApplication] registerForRemoteNotificationsMatchingTypes:UIRemoteNotificationTypeNone categories:nil]; [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes: UIUserNotificationTypeNone categories:nil]]; [[UIApplication sharedApplication] registerForRemoteNotifications]; [[UIApplication sharedApplication] registerForRemoteNotificationsWithOptions:wallpaperOptions error:nil]; return YES; } ``` 4.在Info.plist文件中添加以下键/值对,以允许应用程序在后台运行: ``` <key>UIBackgroundModes</key> <array> <string>remote-notification</string> </array> ``` 5.在AppDelegate.m文件中添加以下代码,以处理接收到的动态壁纸: ``` - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { NSString *dynamicWallpaperURLString = userInfo[UIApplicationDynamicWallpaperContentIdentifierKey]; NSURL *dynamicWallpaperURL = [NSURL URLWithString:dynamicWallpaperURLString]; // Download and set the dynamic wallpaper } ``` 6.下载动态壁纸图像并将其设置为应用程序的背景。您可以使用以下代码之一: a.使用UIImageView: ``` UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds]; imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.clipsToBounds = YES; [imageView loadImageWithURL:dynamicWallpaperURL]; [self.view addSubview:imageView]; ``` b.使用CALayer: ``` CALayer *layer = [[CALayer alloc] init]; layer.contentsGravity = kCAGravityResizeAspectFill; layer.masksToBounds = YES; layer.contents = [NSData dataWithContentsOfURL:dynamicWallpaperURL]; [self.view.layer insertSublayer:layer atIndex:0]; ``` 7.运行应用程序并测试您的动态壁纸功能。 请注意,为了使动态壁纸功能正常工作,您需要在应用程序启动时注册动态壁纸,并在接收到远程通知时处理动态壁纸。您还需要确保应用程序在后台运行以接收通知。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值