手势识别器

手势识别器的实现和方法

手势识别器的应用主要是针对图片,所以在文件内部先声明了一个UIImageView对象,用来进行下面的模拟

//声明的UIImageView
@property(nonatomic,retain)UIImageView *imageView;
//以下为UIImageView对象的准备工作
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];
//把图片的用户交互打开,它默认是关闭的,此外还有一个控件是lable
//下面这段代码如果不写,所有针对图片的操作均不能使用
self.imageView.userInteractionEnabled = YES;

所有的手势声明全部写在viewDidLoad

所有手势声明后实现的方法写在viewDidLoad外面

点击方法的实现与声明

//1.点击手势的声明
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(@"测试一下点击手势");
//当触发这个方法的时候,imageview现实的图片会更换
    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
//具体各项意义与AlertView相同
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;
//进行旋转的操作
//通过视图的transform属性,让试图进行旋转
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;
//通过transform改变图片的尺寸
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; //每次递增2
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");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值