变形
变形 (transform):
什么是变形
视图发生了 位移, 缩放, 旋转这样的变化叫做变形
如何实现视图的变形
通过修改视图对象 transform属性就能完成变形
transform属性
** 类型** CGAffineTransform (结构体类型)
** 修改transform**
1、translation 位移
2、scale 缩放
3、rotation 旋转
4、CGAffineTransformMakeTranslation //位移变化,变形是基于变形前的那个基础状态
CGAffineTransformTranslate
CGAffineTransformMakeScale
CGAffineTransformScale
CGAffineTransformMakeRotation
CGAffineTransformRotate
清除视图的所有变形
CGAffineTransformIdentity
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
//位移变化
//CGAffineTransformMakeTranslation 变形是基于变形前的那个基础状态
// self.imageView.transform = CGAffineTransformMakeTranslation(50, 50);
//变形是在 当前 变形基础上 继续变形
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, 50, 50);
//缩放变形
// self.imageView.transform = CGAffineTransformMakeScale(1.1, 1.5);
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, 1.02, 1.02);
//旋转变化
// self.imageView.transform = CGAffineTransformMakeRotation(M_PI_4);
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, M_PI_4);//45度
NSLog(@"transform %@",NSStringFromCGAffineTransform(self.imageView.transform));
}
bounds:
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@end
@implementation ViewController
//手势代理方法 返回YES是可以同时触发
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
//拖拽
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGR:)];
self.imageView.userInteractionEnabled = YES;
[self.view addGestureRecognizer:panGR];
//捏合
UIPinchGestureRecognizer *pinchGR = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchGR:)];
pinchGR.delegate = self;
[self.view addGestureRecognizer:pinchGR];
//旋转
UIRotationGestureRecognizer *rotationGR = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGR:)];
rotationGR.delegate = self;
[self.view addGestureRecognizer:rotationGR];
//点击
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGR:)];
[self.view addGestureRecognizer:tapGR];
}
-(void)tapGR:(UITapGestureRecognizer*)gr {
self.imageView.transform = CGAffineTransformIdentity;
}
-(void)rotationGR:(UIRotationGestureRecognizer*)gr {
CGFloat rotation = gr.rotation;
self.imageView.transform = CGAffineTransformRotate(self.imageView.transform, rotation);
gr.rotation = 0;
}
-(void)pinchGR:(UIPinchGestureRecognizer*)gr {
CGFloat scale = gr.scale;
self.imageView.transform = CGAffineTransformScale(self.imageView.transform, scale, scale);
gr.scale = 1;
}
-(void)panGR:(UIPanGestureRecognizer*)gr {
// CGPoint translation = [gr translationInView:self.view];
// CGPoint center = self.imageView.center;
// center.x += translation.x;
// center.y += translation.y;
// self.imageView.center = center;
// [gr setTranslation:CGPointZero inView:self.view];
CGPoint translation = [gr translationInView:self.view];
self.imageView.transform = CGAffineTransformTranslate(self.imageView.transform, translation.x, translation.y);
[gr setTranslation:CGPointZero inView:self.view];
}
深入坐标系:
- (void)viewDidLoad {
[super viewDidLoad];
[self printInfo];
NSLog(@"============================");
[self changeTransform];
}
-(void)printInfo{
NSLog(@"\nframe:%@\nbounds:%@\ncenter:%@\ntransform:%@\n",
NSStringFromCGRect(self.myView.frame),
NSStringFromCGRect(self.myView.bounds),
NSStringFromCGPoint(self.myView.center),
NSStringFromCGAffineTransform(self.myView.transform)
);
}
-(void)changeFrame {
CGRect frame = self.myView.frame;
frame.origin.x += 10;
frame.origin.y += 10;
frame.size.width += 100;
frame.size.height += 100;
self.myView.frame = frame;
[self printInfo];
}
-(void)changeBounds {
CGRect bounds = self.myView.bounds;
// bounds.origin.x += 10;
// bounds.origin.y += 10;
bounds.size.width += 100;
bounds.size.height += 100;
self.myView.bounds = bounds;
[self printInfo];
}
-(void)changeCenter {
CGPoint center = self.myView.center;
center.x += 100;
center.y += 100;
self.myView.center = center;
[self printInfo];
}