UISwipeGestureRecognizer with UIView 在单独的类中创建不起作用



我在单独的类中创建了一个UIView,我正在尝试在我的ViewController中对其进行动画处理。它应该像向下滑动时出现的iPhone上的通知屏幕一样工作,然后您可以向上滑动。

我可以让我的自定义视图向下轻扫,但是当我尝试向上轻扫它时,未启动向上轻扫手势。

我是新手,所以任何帮助都非常感谢!

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    NotificationView *notificationView = [[NotificationView alloc]init];
    [self.view addSubview:notificationView];

    UISwipeGestureRecognizer *swipeDownGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeDown:)];
    swipeDownGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
    UISwipeGestureRecognizer *swipeUpGestureRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipeUp:)];
    swipeUpGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
    [self.view addGestureRecognizer:swipeDownGestureRecognizer];
    [notificationView addGestureRecognizer:swipeUpGestureRecognizer];
}
-(void) swipeUp: (UISwipeGestureRecognizer *) recognizer{
    UIView *notificationView = [[NotificationView alloc]init];
    notificationView = recognizer.view;
    [UIView animateWithDuration:2.0 animations:^{
        notificationView.frame = CGRectMake(0, 0, 414, 723);
    }];
}
-(void) swipeDown: (UISwipeGestureRecognizer *) recognizer{
    UIView *notificationView = [[NotificationView alloc]init];
    notificationView = recognizer.view;
    [UIView animateWithDuration:2.0 animations:^{
        notificationView.frame = CGRectMake(0, 723, 414, 723);
    }];
}

ViewDidLoad 中的通知视图应分配给 viewControllers 属性,并且不应在手势识别器操作中分配初始化视图。

您应该使用 notificationView 创建一个属性并保留对它的引用,而不是一遍又一遍地创建一个新属性。

@property (strong, nonatomic) NotificationView *notificationView;

在你的viewDidLoad

       _notificationView = [[NotificationView alloc] init];
// Important line to solve your problems on gestures not fired off on your notification view
_notificationView.userInteractionEnabled = YES;
        [self.view addSubview:_notificationView];

并简单地更改:

-(void)swipeDown:(UISwipeGestureRecognizer *)recognizer {
    [UIView animateWithDuration:2.0 animations:^{
        _notificationView.frame = CGRectMake(0, 723, 414, 723);
    }];
}

您应该查看有关属性如何工作的教程。您应该查看此线程,以避免通过处理手势状态多次调用动画等。

最新更新