用基础动画实现iOS控件循环旋转

本文将指导您如何在SwiftUI中使用Objective-C创建一个可以进行360度旋转的按钮。通过实现旋转动画,您可以为应用程序添加交互性的视觉效果。我们将使用Objective-C作为桥梁,结合SwiftUI的功能来完成这一任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

- (void)viewDidLoad {
    [super viewDidLoad];    
    UIButton* ag=[[UIButton alloc]initWithFrame:CGRectMake(screenWidth/2-75, 174, 150, 150)];
    UIImage* img=[UIImage imageNamed:@"m_01"];
    [ag setImage:img forState:UIControlStateNormal];
    ag.backgroundColor=[UIColor redColor];
    [self rotate360DegreeWithButton:ag];
    [self.view addSubview:ag];
}

- (UIButton*)rotate360DegreeWithButton:(UIButton*)button
{
    CABasicAnimation *animation=[CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    animation.toValue = [NSNumber numberWithFloat:M_PI];
    animation.duration = 10;
    //旋转效果累计 先转180度 接着再旋转180度 从而实现360旋转
    animation.cumulative = YES;
    animation.repeatCount = 1000;
    [button.layer addAnimation:animation forKey:@"transform.rotation.z"];
    return button;
}

 

转载于:https://www.cnblogs.com/death3721/p/5032589.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值