iOS-- 手势及transform属性的使用方法的总结
2016-06-03 本文已影响1204人
钎探穗
CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放、旋转和平移操作.transform我们一般称为形变属性,其本质是通过矩阵变化改变控件的大小、位置、角度等.
- transform进行旋转、缩放、移动的时候不是在原来的基础上增量形变的,因此如果需要持续在原来的基础上旋转、缩放、移动那么每次需要在原来的基础上增加或减少。
这些iOS已经为我们提供好了三个对应的方法(可以定义一个全局变量进行累加),
基于transform参数的形变[
CGAffineTransformRotate
、CGAffineTransformScale
、CGAffineTransformTranslate
]相比之前的基于控件初始位置的形变[CGAffineTransformMakeRotation
(旋转)、CGAffineTransformMakeScale
(缩放)、CGAffineTransformMakeTranslation
(移动)]不会出现复原式的抖动,能停留在改动后的位置.
下面给出一些代码可以自己操作,看下结果:
pragma mark tap 轻拍
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
//设置手势相关属性
//轻拍的次数
tap.numberOfTapsRequired = 1;
//手指数1下
tap.numberOfTouchesRequired = 1;
// [self.view addGestureRecognizer:tap];
//轻拍事件
- (void)tapAction:(UITapGestureRecognizer *)sender{
sender.view.backgroundColor = [UIColor colorWithRed:cKol green:cKol blue:cKol alpha:1];
}
pragma mark swipe 清扫
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft|UISwipeGestureRecognizerDirectionRight; //"|":表示和的意思.
//[mview addGestureRecognizer:swipe];
//清扫事件
- (void)swipeAction:(UISwipeGestureRecognizer *)sender{
sender.view.backgroundColor = [UIColor colorWithRed:cKol green:cKol blue:cKol alpha:1];
}
pragma mark pan 平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
//[mview addGestureRecognizer:pan];
//平移事件
- (void)panAction:(UIPanGestureRecognizer *)sender{
CGPoint piont = [sender translationInView:sender.view];
//三个参数,会发生平移
sender.view.transform = CGAffineTransformTranslate(sender.view.transform, piont.x, piont.y);
//设置增量为0 ;
[sender setTranslation:CGPointZero inView:sender.view];
}
pragma mark pinch 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[mview addGestureRecognizer:pinch];
- (void)pinchAction:(UIPinchGestureRecognizer *)sender{
//两个参数,每次缩放从原始大小开始
//三个参数,每次缩放从上一次缩放的大小开始
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
//设置缩放比例(1:正常. <1:缩放. >1:放大)
sender.scale = 0.5;
}
pragma mark rotation 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[mview addGestureRecognizer:rotation];
}
- (void)rotationAction:(UIRotationGestureRecognizer *)sender{
sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
//清除增量
sender.rotation =0;
}