手势识别器的实现和方法
手势识别器的应用主要是针对图片,所以在文件内部先声明了一个UIImageView对象,用来进行下面的模拟
@property(nonatomic,retain)UIImageView *imageView;
UIImage *image = [UIImage imageNamed:@"123.jpg"];
self.imageView = [[UIImageView alloc]initWithImage:image];
self.imageView.frame = CGRectMake(0, 200, 300, 400);
[self.view addSubview:self.imageView];
[_imageView release];
self.imageView.userInteractionEnabled = YES;
所有的手势声明全部写在viewDidLoad
所有手势声明后实现的方法写在viewDidLoad外面
点击方法的实现与声明
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget: self action:@selector(tapAction:)];
tap.numberOfTapsRequired = 2;
tap.numberOfTouchesRequired = 2;
[self.imageView addGestureRecognizer:tap];
[tap release];
#pragma mark 点击的方法
-(void)tapAction:(UITapGestureRecognizer *)tap{
NSLog(@"测试一下点击手势");
self.imageView.image = [UIImage imageNamed:@"456.jpg"];
}
长按方法的实现与声明
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
longPress.minimumPressDuration = 1;
longPress.allowableMovement = 200;
[self.imageView addGestureRecognizer: longPress];
[longPress release];
#pragma mark 长按对应的响应方法
-(void)longPressAction:(UILongPressGestureRecognizer *)longPress{
longPress.state
UIAlertView *view = [[UIAlertView alloc]initWithTitle:@"长按" message:@"长安"delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[view show];
[view release];
旋转方法的实现与声明
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
[self.imageView addGestureRecognizer:rotation];
[rotation release];
#pragma mark 通过图片的旋转手势,让图片发生旋转
-(void)rotationAction:(UIRotationGestureRecognizer *)rotation{
UIImageView *imageView = (UIImageView *)rotation.view;
imageView.transform = CGAffineTransformRotate(imageView.transform, rotation.rotation);
rotation.rotation = 0;
}
捏合方法的实现与声明
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchAction:)];
[self.imageView addGestureRecognizer:pinch];
[pinch release];
#pragma mark 通过图片的捏合,让图片缩放
-(void)pinchAction:(UIPinchGestureRecognizer *)pinch{
UIImageView *imageView = (UIImageView *)pinch.view;
imageView.transform = CGAffineTransformScale(imageView.transform, pinch.scale, pinch.scale);
pinch.scale = 1;
}
拖拽方法的实现与声明
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[self.imageView addGestureRecognizer:pan];
[pan release];
#pragma mark 通过拖拽让图片移动
-(void)panAction:(UIPanGestureRecognizer *)pan{
UIImageView *imageView = (UIImageView *)pan.view;
CGPoint p = [pan translationInView:imageView];
imageView.transform = CGAffineTransformTranslate(imageView.transform, p.x, p.y);
[pan setTranslation:CGPointZero inView:imageView];
}
轻扫方法的实现与声明(显示效果是一对加减号)
创建轻扫手势的对象
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeAction:)];
[self.imageView addGestureRecognizer:swipe];
[swipe release];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
#pragma mark 通过轻扫的对应方法
-(void)swipeAction:(UISwipeGestureRecognizer *)swipe{
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"向右");
}
}
步进控件
UIStepper *stepper = [[UIStepper alloc] init];
stepper.tag = 10;
stepper.center = CGPointMake(160, 240);
stepper.tintColor = [UIColor grayColor];
stepper.minimumValue = 0;
stepper.maximumValue = 1;
stepper.stepValue = 0.1;
stepper.value = 0;
[stepper setWraps:YES];
[stepper addTarget:self action:@selector(doTest) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:stepper];
[stepper release];
self.imageView.alpha = 0;
-(void)doTest
{
UIStepper *per = (UIStepper*)[self.view viewWithTag:10];
self.imageView.alpha = per.value;
if (per.continuous)
{
NSLog(@"Y");
int a = per.value;
NSLog(@"%d", a);
}
else{
NSLog(@"N");
}
}