#import"GCViewController.h"
//触控手势:
@interface GCViewController ()
@end
@implementation GCViewController
{
UIImageView *imageView;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self initImageView];
}
- (void)initImageView{
imageView = [[UIImageView alloc] init];
imageView.frame = CGRectMake(100, 150, 100, 150);
imageView.image = [UIImage imageNamed:@"back2.jpg"];
[self.view addSubview:imageView];
imageView.userInteractionEnabled = YES;
[self creatPinges];
[self creatRota];
[self createPan];
[self creatLopngpress];
}
//*********************放大:**************//
- (void)creatPinges{
UIPinchGestureRecognizer *pinGes = [[UIPinchGestureRecognizer alloc] init];
[pinGes addTarget:self action:@selector(pinClick:)];
pinGes.delegate = self;
[imageView addGestureRecognizer:pinGes];
}
- (void)pinClick:(UIPinchGestureRecognizer *)pinGes{
//参数2,x方向的缩放比例
//参数3,y方向的缩放比例
pinGes.view.transform = CGAffineTransformScale(pinGes.view.transform,pinGes.scale, pinGes.scale);
//每次缩放的比例都设为,下次的缩放比例是基于上次的缩放结果。
pinGes.scale = 1;
}
//*********************移动:**************//
- (void)creatRota{
UIRotationGestureRecognizer *rotaGes = [[UIRotationGestureRecognizer alloc] init];
[rotaGes addTarget:self action:@selector(rotaClick:)];
rotaGes.delegate = self;
[imageView addGestureRecognizer:rotaGes];
}
- (void)rotaClick:(UIRotationGestureRecognizer *)rotaGes{
rotaGes.view.transform = CGAffineTransformRotate(rotaGes.view.transform, rotaGes.rotation);
rotaGes.rotation = 0;
}
//*********************旋转:**************//
- (void)createPan{
UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] init];
[panGes addTarget:self action:@selector(panCLick:)];
[imageView addGestureRecognizer:panGes];
}
- (void)panCLick:(UIPanGestureRecognizer *)pan{
CGPoint point = [pan translationInView:self.view];
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
[pan setTranslation:CGPointMake(0, 0) inView:self.view];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//*********************长按:**************//
- (void)creatLopngpress{
UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] init];
[longGes addTarget:self action:@selector(longClick:)];
[imageView addGestureRecognizer:longGes];
}
- (void)longClick:(UILongPressGestureRecognizer *)longGes{
//按压达到0.5秒会触发操作,此次按压松开之后还会再次触发一次。
//设置触发时长:
longGes.minimumPressDuration = 1;//按压一秒之后会触发。
NSLog(@"123456");
}
//是否支持多个手势:
//这个方法的在用户操作时,判定为一种手势的同时还能判定为另一种手势。
//
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end