滑动手势在 UITableViewCell 中不起作用
Swipe gesture not working in UITableViewCell
我的 ViewController
中有 UITableView
。有一个习俗UITableViewCell
。在那个习惯 cell
中,我有一个 UIImageView
和多个 UILabel
。我想在 UIImageView
上添加 UISwipeGestureRecognizer
。我已经以编程方式采取手势,但它不起作用。
这是我完成的代码:
////////// THIS CODE I TOOK IN CELL FOR ROW METHOD:
swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(Swipe_Handling:)];
swipe.direction=UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[cells.propimage addGestureRecognizer:swipe];
////////////
-(void)Swipe_Handling:(UISwipeGestureRecognizer *)recognizer
{
if (recognizer.direction==UISwipeGestureRecognizerDirectionRight)
{
NSLog(@"Right");
}
else if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft)
{
NSLog(@"Left");
}
}
滑动处理操作被调用,但它不会在 if-else 条件下进行并且 cells.propimage.userInteractionEnabled=YES;
。
当我打印时 NSLog(@"%lu",(unsigned long)recognizer.direction);
它returns我:3
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
};
请在此添加手势委托方法
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
这条线用你的手势它会起作用
[yourgest setCancelsTouchesInView:NO];
它会起作用..
请为 2 个方向尝试此代码
UISwipeGestureRecognizer* gestureR;
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
[imgview addGestureRecognizer:gestureR];
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionRight; // default
[imgview addGestureRecognizer:gestureR];
我在单元格的 awakeFromNib() 中添加了 "add gesture code",它有效
override func awakeFromNib() {
super.awakeFromNib()
// add gesture here
}
我的 ViewController
中有 UITableView
。有一个习俗UITableViewCell
。在那个习惯 cell
中,我有一个 UIImageView
和多个 UILabel
。我想在 UIImageView
上添加 UISwipeGestureRecognizer
。我已经以编程方式采取手势,但它不起作用。
这是我完成的代码:
////////// THIS CODE I TOOK IN CELL FOR ROW METHOD:
swipe=[[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(Swipe_Handling:)];
swipe.direction=UISwipeGestureRecognizerDirectionLeft | UISwipeGestureRecognizerDirectionRight;
[cells.propimage addGestureRecognizer:swipe];
////////////
-(void)Swipe_Handling:(UISwipeGestureRecognizer *)recognizer
{
if (recognizer.direction==UISwipeGestureRecognizerDirectionRight)
{
NSLog(@"Right");
}
else if (recognizer.direction==UISwipeGestureRecognizerDirectionLeft)
{
NSLog(@"Left");
}
}
滑动处理操作被调用,但它不会在 if-else 条件下进行并且 cells.propimage.userInteractionEnabled=YES;
。
当我打印时 NSLog(@"%lu",(unsigned long)recognizer.direction);
它returns我:3
typedef NS_OPTIONS(NSUInteger, UISwipeGestureRecognizerDirection) {
UISwipeGestureRecognizerDirectionRight = 1 << 0,
UISwipeGestureRecognizerDirectionLeft = 1 << 1,
UISwipeGestureRecognizerDirectionUp = 1 << 2,
UISwipeGestureRecognizerDirectionDown = 1 << 3
};
请在此添加手势委托方法
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return YES;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
return YES;
}
这条线用你的手势它会起作用
[yourgest setCancelsTouchesInView:NO];
它会起作用..
请为 2 个方向尝试此代码
UISwipeGestureRecognizer* gestureR;
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionLeft;
[imgview addGestureRecognizer:gestureR];
gestureR = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom)] autorelease];
gestureR.direction = UISwipeGestureRecognizerDirectionRight; // default
[imgview addGestureRecognizer:gestureR];
我在单元格的 awakeFromNib() 中添加了 "add gesture code",它有效
override func awakeFromNib() {
super.awakeFromNib()
// add gesture here
}