iOS控件的缩放,平移,旋转和回到原点

这篇博客详细介绍了如何在Objective-C中实现iOS控件的平移、旋转和缩放操作。通过实例代码展示了如何使用CGAffineTransform进行动画效果,包括平移、旋转和缩放,并提供了将控件恢复到初始状态的方法。

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

<span style="font-family:Verdana;"><span style="font-family:Microsoft YaHei;"><span style="font-family:Microsoft YaHei;">#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIButton *moving;

- (IBAction)move;

- (IBAction)rotate;

- (IBAction)scale;

- (IBAction)goback;

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)move {
    
    // 2.修改结构体值
    // 下面这句话的意思是:告诉控件,要平移到距离原始位置-50的位置
//    self.moving.transform = CGAffineTransformMakeTranslation(0, 50); // 向上平移
    
    // 基于一个旧的值,再进行平移
    // 基于一个现有的值,再进行平移
    self.moving.transform = CGAffineTransformTranslate(self.moving.transform, 0, 50);
}

- (IBAction)rotate {
    //45°
//    self.moving.transform = CGAffineTransformMakeRotation(M_PI_4);
    self.moving.transform = CGAffineTransformRotate(self.moving.transform, M_PI_4);
}

- (IBAction)scale {
//    self.moving.transform = CGAffineTransformMakeScale(0.5, 0.5);
    
    [UIView animateWithDuration:2.5 animations:^{
        self.moving.transform = CGAffineTransformTranslate(self.moving.transform, 0, 50);
        self.moving.transform = CGAffineTransformRotate(self.moving.transform, M_PI_4);
        self.moving.transform = CGAffineTransformScale(self.moving.transform, 1.5, 1.5);
    }];
}

//让控件回到原来的位置
- (IBAction)goback {
    self.moving.transform = CGAffineTransformIdentity;
}
@end
</span></span></span>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值