IOS开发—CGAffineTransform的使用大概

IOS中CGAffineTransform的使用大概

CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放、旋转和平移操作:

总得来说,这个类中包含3张不同类型,分别使用如下3个方法创建数值;

1.CGAffineTransformMakeTranslation(CGFloat tx, CGFloat ty)(平移:设置平移量)

2.CGAffineTransformMakeScale(CGFloat sx, CGFloat sy)(缩放:设置缩放比例)仅通过设置缩放比例就可实现视图扑面而来和缩进频幕的效果。

3.CGAffineTransformMakeRotation(CGFloat angle)(旋转:设置旋转角度)

这3种方法分别对应另外一种方法:

1.CGAffineTransformMakeTranslation

2.CGAffineTransformMakeRotation

3.CGAffineTransformMakeScale

区别在于:下面这种方法这种方法每次形变都不能叠加形变,所以再次触发形变动作时形变会先还原,而上面这种方法每次都是以传入的transform为参照(既有叠加效果)

以上3个都是针对视图的原定最初位置的中心点为起始参照进行相应操作的,在操作结束之后可对设置量进行还原

view.transform=CGAffineTransformIdentity;

 

配合手势代码示例:

#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) UIView *myView;
@end
@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.myView];
    [self addPanGesture];
    [self addRotationGesture];
    [self addPinchGesture];
}

- (UIView *)myView{
    if (!_myView) {
        _myView = [[UIView alloc]initWithFrame:CGRectMake(60, 100, [UIScreen mainScreen].bounds.size.width-120, 80)];
        _myView.backgroundColor = [UIColor greenColor];
    }
    return _myView;
}
 
- (void)addPanGesture{
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [_myView addGestureRecognizer:pan];
}
 
- (void)addRotationGesture{
    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
    [_myView addGestureRecognizer:rotation];
}
 
- (void)addPinchGesture{
    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
    [_myView addGestureRecognizer:pinch];
}
//退拽平移
- (void)panAction:(UIPanGestureRecognizer *)pan{
    NSLog(@"pan");
    CGPoint offPoint = [pan translationInView:pan.view];
<p class="p1"><span class="s1">/**</span><span class="s2">这种方法这种方法每次形变都不能叠加形变,所以再次触发形变动作时形变会先还原</span><span class="s1">**/</span></p>//    pan.view.transform =CGAffineTransformMakeTranslation(offPoint.x, offPoint.y);
    <p class="p1"><span class="s1">/**</span><span class="s2">这种方法每次都是以传入的</span><span class="s1">transform</span><span class="s2">为参照(既有叠加效果)</span><span class="s1">**/</span></p>    pan.view.transform = CGAffineTransformTranslate(pan.view.transform, offPoint.x, offPoint.y);
    //每一次平移后,产生X轴和Y轴的增量要归零
    [pan setTranslation:CGPointZero inView:pan.view];
}
//旋转
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation{
    NSLog(@"rotation");
//    rotation.view.transform =CGAffineTransformMakeRotation(rotation.rotation);
    rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
    //每次旋转完成后,设置旋转弧度为0
    rotation.rotation = 0;
}
//捏合缩放
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch{
    NSLog(@"pinch");
//    pinch.view.transform =CGAffineTransformMakeScale(pinch.scale, pinch.scale);
    pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
    //每次缩放完成后,比例设置成1;
    pinch.scale = 1;
}
@end

另外,对于控件的形变是以中心点为基准点的,但是对于XIB中创建的控件的形变却是以左上角(00)点为基准点,原因不明。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值