Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

为View添加手势

$
0
0
#import <UIKit/UIKit.h>

@interface TestGestureRecognizerViewController : UIViewController {
    UIView *aView;
}
@property(nonatomic,retain)IBOutlet UIView *aView;
@end

///////////////////////////////////////////////////////////////////////////////////////////////


#import "TestGestureRecognizerViewController.h"
#import <QuartzCore/QuartzCore.h>//导入框架

@implementation TestGestureRecognizerViewController
@synthesize aView;

-(void)handelPan:(UIPanGestureRecognizer*)gestureRecognizer{
    //获取平移手势对象在self.view的位置点,并将这个点作为self.aView的center,这样就实现了拖动的效果
    CGPoint curPoint = [gestureRecognizer locationInView:self.view];
    [self.aView setCenter:curPoint];
}
-(void)handelTap:(UITapGestureRecognizer *)gestureRecognizer{
    NSLog(@"%s",__FUNCTION__);
    [NSRunLoop cancelPreviousPerformRequestsWithTarget:self];//双击事件取消延时
}
//单击方法
-(void)handelSingleTap:(UITapGestureRecognizer*)gestureRecognizer{
    NSLog(@"%s",__FUNCTION__);
    [self performSelector:@selector(singleTap:) withObject:nil afterDelay:0.2];
}
-(void)singleTap:(id)sender{
    NSLog(@"%s",__FUNCTION__);
}

- (void)viewDidLoad {
    [super viewDidLoad];

    //创建一个平移手势对象,该对象可以调用handelPan:方法
    UIPanGestureRecognizer *panGes = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handelPan:)];
    [self.aView addGestureRecognizer:panGes];
    [panGes release];

    //创建一个点击手势对象,该对象可以调用handelTap:方法
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelTap:)];
    [self.view addGestureRecognizer:tapGes];
    [tapGes release];
    [tapGes setNumberOfTouchesRequired:1];//触摸点个数
    [tapGes setNumberOfTapsRequired:2];//点击次数

    //创建一个点击手势对象,该对象可以调用handelSingleTap:方法
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handelSingleTap:)];
    [self.view addGestureRecognizer:singleTap];
    [singleTap release];
    [singleTap setNumberOfTouchesRequired:1];//触摸点个数
    [singleTap setNumberOfTapsRequired:1];//点击次数
}

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

@end

作者:zzzili 发表于2013-1-5 14:21:37 原文链接
阅读:42 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>