我试图了解手势识别器的工作原理,并尝试让手势识别器告诉我何时长时间触摸BEGINS以及何时即使触摸不动也会结束。
在viewDidLoad ,我添加了一个名为game的子视图。 在游戏中,我已经实现了两种识别触摸的方法。 一个有效,但没有做我想要的。 另一个不起作用。
Method1我刚刚添加了两个方法:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Began"); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *) event { NSLog(@"Ended"); }这有效,无需实例化手势识别器。 有人可以告诉我为什么吗?
这个问题是- (void)touchedEnded:只有在触摸移动时才会被调用,而如果触摸在它开始的同一位置结束则不会被调用。 因此,如果我触摸并移动并放开,两个函数都会被调用。 如果我触摸并按住并放开(不移动),只会调用- (void)touchesBegan 。
方法2我实例化了一个手势识别器:
@property (nonatomic, strong) UILongPressGestureRecognizer *lprg;然后在我的设置中:
self.lprg = [UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];然后:
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer { NSLog(@"Handling"); switch (recognizer.state) { case UIGestureRecognizerStateBegan: // ... } }但是这个我以编程方式实例化识别器的方法并不起作用。 我从未在控制台中获得任何NSLog输出。
I am trying to understand how the gesture recognizer works and trying to get a gesture recognizer to tell me when a long touch BEGINS and when it ENDS even if the touch does not move.
In viewDidLoad, I add a subview called game. In game, I have implemented two ways to recognize touches. One works but does not do what I want. The other does not work.
Method1 I just added two methods:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { NSLog(@"Began"); } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *) event { NSLog(@"Ended"); }This worked, without instantiating a gesture recognizer. Can someone tell me why?
The issue with this is that - (void)touchedEnded: only gets called if the touch moved and not if the touch ended at the same location it started. So if I touched and moved and let go, both functions get called. If I touch and hold and let go (without moving), only the - (void)touchesBegan gets called.
METHOD 2 I instantiated a gesture recognizer:
@property (nonatomic, strong) UILongPressGestureRecognizer *lprg;then in my setup:
self.lprg = [UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];then:
- (void)handleLongPress:(UILongPressGestureRecognizer *)recognizer { NSLog(@"Handling"); switch (recognizer.state) { case UIGestureRecognizerStateBegan: // ... } }But this one, where I programmatically instantiated the recognizer, did not work. I never got any NSLog output in the console.
最满意答案
你必须使用这个代码来初始化手势
UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(celllongpressed:)]; [gesture1 setDelegate:self]; [gesture1 setMinimumPressDuration:1]; [self addGestureRecognizer:gesture1];和目标方法使用这个
-(void)celllongpressed:(UIGestureRecognizer *)longPress { }you have to use this code to intialized gesture
UILongPressGestureRecognizer *gesture1 = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(celllongpressed:)]; [gesture1 setDelegate:self]; [gesture1 setMinimumPressDuration:1]; [self addGestureRecognizer:gesture1];and to target method use this
-(void)celllongpressed:(UIGestureRecognizer *)longPress { }更多推荐
发布评论