我使用了以下代码,但是当我运行程序时,滑动无效。
首先我在界面部分添加了手势识别器协议
#import <UIKit/UIKit.h>
@interface BNRViewController : UIViewController <UIGestureRecognizerDelegate>
@property (nonatomic,strong) IBOutlet UILabel *swipe;
@end
现在我在工具部分
中实例化了滑动手势识别器- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(fingerSwipe:)];
swipeUp.numberOfTouchesRequired = 3;
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUp];
}
任何人都可以帮助我
答案 0 :(得分:5)
除非您在标签上设置 userInteractionEnabled为YES ,否则手势识别器将无法使用。
所以你必须添加这行[swipe setUserInteractionEnabled:YES];
它正在检查。
UISwipeGestureRecognizer *swipeUp = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(fingerSwipe:)];
[swipe setUserInteractionEnabled:YES];
swipeUp.numberOfTouchesRequired = 1;
swipeUp.direction = UISwipeGestureRecognizerDirectionUp;
[self.view addGestureRecognizer:swipeUp];
答案 1 :(得分:0)
可能为视图定义了 1 个以上的手势
你可以用这个来检查;
self.view.gestureRecognizers.count
删除所有手势并添加您的滑动手势,然后重试
self.view.gestureRecognizers?.removeAll()
self.view.addGestureRecognizer(swipeGestureRecognizer)