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中创建的控件的形变却是以左上角(0,0)点为基准点,原因不明。
原文地址:http://blog.csdn.net/lotheve/article/details/46491607