在.h文件里创建一个视图对象

在.h文件里创建一个视图对象。在.h文件里创建一个视图对象。手势是指你用二个或两个手指接触荧屏初叶,直到你的手指头全部离开显示器结束所发生的持有事件。手势识别器(UIGestureRecognizer)是多个对象,知道什么样观望客商生成的事件流,并识别客户何时以与预定义的手势相相配的法子开展拉触摸和拖动。UIGestureRecognizer类封装了探寻手势的干活。在模拟器中,按“option”键,可效仿两个手指头的手势。

在.h文件里创设二个视图对象,全体的手势都以在视图上开展的:

@interface LinViewController : UIViewController
//创建视图对象
@property (retain, nonatomic) UIView * mView;

@end

在.m文件里,创造各样手势的靶子,编写各样手势的完成方式:

@implementation LinViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //---点击手势识别器---//
    //创建点击手势的对象
    UITapGestureRecognizer * pSingleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleGesture:)];
    //将手势添加到当前视图中
    [self.view addGestureRecognizer:pSingleTap];

    //创建点击手势的对象
    UITapGestureRecognizer * pDoubleTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleGesture:)];
    //设置点击次数为2次,默认为1次,属于单击操作
    pDoubleTap.numberOfTapsRequired = 2;
    //将手势添加到当前视图中
    [self.view addGestureRecognizer:pDoubleTap];
    //双击的时候让单击放弃响应
    [pSingleTap requireGestureRecognizerToFail:pDoubleTap];
    //释放创建的对象
    [pDoubleTap release];
    [pSingleTap release];

    //---滑动手势识别器---//
    //创建滑动手势的对象
    UISwipeGestureRecognizer * pSwip = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipGesture:)];
    //设置滑动的方向,此处为向右,共有4个方向
    pSwip.direction = UISwipeGestureRecognizerDirectionRight;
    //将手势添加到当前视图中
    [self.view addGestureRecognizer:pSwip];
    //释放创建的对象
    [pSwip release];

    //---拖动手势识别器--//
    //创建拖动手势对象
    UIPanGestureRecognizer * pPan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panGesture:)];
    //将手势添加到当前视图中
    [self.view addGestureRecognizer:pPan];
    //释放创建的对象
    [pPan release];

    //---长按手势识别器--//
    //创建长按手势的对象
    UILongPressGestureRecognizer * pLongPress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressGesture:)];
    //设置长按的时间,2秒以上判定为长按
    pLongPress.minimumPressDuration = 2;
    //将手势添加到当前视图中
    [self.view addGestureRecognizer:pLongPress];
    //释放创建的对象
    [pLongPress release];

    //---旋转手势识别器---//
    //创建旋转手势对象
    UIRotationGestureRecognizer * pRotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationGesture:)];
    //将手势添加到当前视图中
    [self.view addGestureRecognizer:pRotation];
    //释放创建的对象
    [pRotation release];
}
#pragma mark--------tapGesture>>>single  单击
- (void)singleGesture:(UITapGestureRecognizer *)tap
{
    //单击手势对应的操作
    NSLog(@"单击操作");
}
#pragma mark--------tapGesture>>>double  双击
- (void)doubleGesture:(UITapGestureRecognizer *)tap
{
    //双击手势对应的操作
    NSLog(@"双击操作");
}
#pragma mark--------tapGesture>>>Swip    滑动
- (void)swipGesture:(UISwipeGestureRecognizer *)swip
{
    //滑动,划屏手势对应的操作
    NSLog(@"滑动操作,分方向,此处向右滑动");
}
#pragma mark--------tapGesture>>>Pan     平移
- (void)panGesture:(UIPanGestureRecognizer *)pan
{
    //获取平移手势在视图上的坐标
    CGPoint point = [pan locationInView:self.view];
    //把视图的中心点确定在平移手势的坐标上,即把视图拖动到平移点
    self.mView.center = point;
    NSLog(@"%@",NSStringFromCGPoint(point));
}
#pragma mark--------tapGesture>>>LongPress 长按
- (void)longPressGesture:(UILongPressGestureRecognizer *)longPress
{
    //长按手势对应的操作
    NSLog(@"长按操作");
}
#pragma mark--------tapGesture>>>Rotation  旋转
- (void)rotationGesture:(UIRotationGestureRecognizer *)rotation
{
    //确定手势旋转的大小,角度,使视图做出相应的反应
    float degree = rotation.rotation *(180/M_PI);
    NSLog(@"旋转的角度为%.2f",degree);
}
//释放创建的对象
- (void)dealloc
{
    [_mView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图