标签:uitapgesturerecognizer;uilongpressgesturerecognizer;uiswipegesturerecognizer
.h文件
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
{
UIImageView *imageView;
}
@end
.m文件
#import "RootViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//给View添加背景颜色
self.view.backgroundColor =[UIColorwhiteColor];
//创建UIImageView
imageView =[[UIImageViewalloc]initWithFrame:CGRectMake(80, 100, 100, 100)];
imageView.backgroundColor =[UIColorredColor];
[self.view addSubview:imageView];
//开启交互
imageView.userInteractionEnabled = YES;
// 实现点击方法
UITapGestureRecognizer *tap =[[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tapHand:)];
// 点击次数
tap.numberOfTapsRequired = 1;
[imageView addGestureRecognizer:tap];
[tap release];
// 实现长按方法
UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPress:)];
// 长按需要多长时间才有效
longPress.minimumPressDuration = 2;
// 允许偏移距离
longPress.allowableMovement = 100;
[imageViewaddGestureRecognizer:longPress];
[longPress release];
// 轻扫手势
UISwipeGestureRecognizer *swip =[[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swipPress:)];
// 轻扫的方向,(向左)
swip.direction=UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swip];
[swip release];
// 旋转手势
UIRotationGestureRecognizer *rotation =[[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationP:)];
[imageViewaddGestureRecognizer:rotation];
[rotation release];
// 拖拽方法
UIPanGestureRecognizer *pan =[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(handlePan:)];
[imageView addGestureRecognizer:pan];
[pan release];
}
//点击方法
-(void)tapHand:(UITapGestureRecognizer*)tap
{
}
//长按方法
-(void)longPress:(UILongPressGestureRecognizer *)longP
{
if (longP.state == UIGestureRecognizerStateBegan) {
NSLog(@"开始");
}else if (longP.state == UIGestureRecognizerStateChanged)
{
NSLog(@"移动");
}else if (longP.state == UIGestureRecognizerStateEnded)
{
NSLog(@"结束");
}
}
//轻扫方法
-(void)swipPress:(UISwipeGestureRecognizer*)swip
{
if (swip.direction==UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"图片移动");
}
}
//旋转手势方法
-(void)rotationP:(UIRotationGestureRecognizer *)rotationP
{
rotationP.view.transform=CGAffineTransformRotate(rotationP.view.transform, rotationP.rotation);
rotationP.rotation = 0;
}
//拖拽手势方法
-(void)handlePan:(UIPanGestureRecognizer*)recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center=CGPointMake(recognizer.view.center.x+translation.x,recognizer.view.center.y+translation.y);
[recognizer setTranslation:CGPointZeroinView:self.view];
if(recognizer.state==UIGestureRecognizerStateEnded){
CGPoint velocity = [recognizer velocityInView:self.view];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1*slideMult;// Increase for more of a slide
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),recognizer.view.center.y +(velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0),self.view.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y,0),self.view.bounds.size.height);
[UIViewanimateWithDuration:slideFactor*2 delay:0options:UIViewAnimationOptionCurveEaseOutanimations:^{recognizer.view.center=finalPoint;}completion:nil];
}
}
本文出自 “ios” 博客,请务必保留此出处http://10136044.blog.51cto.com/10126044/1679358
标签:uitapgesturerecognizer;uilongpressgesturerecognizer;uiswipegesturerecognizer
原文地址:http://10136044.blog.51cto.com/10126044/1679358