标签:
这个类是iOS专门用来显示图片的类,几乎所有的图片,都是用这个类显示的。
初始化方法:initWithImage:
UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"2.png"]];
img.frame = CGRectMake(100, 100, 200, 200);
通过图片名来初始化一个UIImageView类。
注意:
ImageView的默认是NO,是关闭交互。要将userInteractionEnabled(响应者链是否交互)设置为YES
self.img.userInteractionEnabled = YES;
手势是加在某个视图上的,否则手势无效。
// 轻拍手势
// 创建轻拍手势识别器
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
// 轻拍手势响应方法
- (void)tapAction:(UITapGestureRecognizer *)sender{
NSLog(@"我是轻拍手势..");
// 换图片
// 获取手势图片
UIImageView *temp = (UIImageView *)sender.view;
temp.image = [UIImage imageNamed:@"3.png"];
}
// 长按手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
longPress.minimumPressDuration = 0.5;
[self.img addGestureRecognizer:longPress];
// 长按响应方法
- (void)longPressAction:(UILongPressGestureRecognizer *)sender{
NSLog(@"长按手势");
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"长按开始..");
[UIView animateWithDuration:1 animations:^{
// 修改view的frame
CGRect temp1 = sender.view.frame;
temp1.size.width +=50;
temp1.size.height +=50;
// 修改完成附回
sender.view.frame = temp1;
sender.view.alpha = 0.5;
}];
}else if (sender.state == UIGestureRecognizerStateEnded){
NSLog(@"长按结束..");
[UIView animateWithDuration:1 animations:^{
// 修改view的frame
CGRect temp1 = sender.view.frame;
temp1.size.width -=50;
temp1.size.height -=50;
// 修改完成附回
sender.view.frame = temp1;
sender.view.alpha = 1;
}];
}
}
// 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[self.img addGestureRecognizer:rotation];
// 旋转手势响应方法
- (void)rotationAction:(UIRotationGestureRecognizer *)sender{
NSLog(@"我是旋转手势..");
sender.view.transform = CGAffineTransformRotate(sender.view.transform, sender.rotation);
sender.rotation = 0;
}
// 捏合
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[self.img addGestureRecognizer:pinch];
// 捏合手势响应方法
- (void)pinchAction:(UIPinchGestureRecognizer *)sender{
NSLog(@"我是捏合手势");
sender.view.transform = CGAffineTransformScale(sender.view.transform, sender.scale, sender.scale);
sender.scale = 1;
}
// 平移
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[self.img addGestureRecognizer:pan];
// 平移手势响应方法
- (void)panAction:(UIPanGestureRecognizer *)sender{
NSLog(@"我是平移手势..");
CGPoint point = [sender translationInView:sender.view];
sender.view.transform = CGAffineTransformTranslate(sender.view.transform, point.x, point.y);
// 将偏移量归零
[sender setTranslation:CGPointZero inView:sender.view];
}
标签:
原文地址:http://www.cnblogs.com/Coder-GT/p/4872557.html