简体   繁体   English

在单独的类中创建的带有UIView的UISwipeGestureRecognizer无法正常工作

[英]UISwipeGestureRecognizer with UIView created in a separate class not working

I created a UIView in a separate class, and I am trying to animate it in my ViewController. 我在一个单独的类中创建了一个UIView,并且尝试在ViewController中对其进行动画处理。 It is supposed to work like the notification screen on the iPhone that appears when you swipe down, and then you can swipe it back up. 它应该可以像向下滑动时出现的iPhone上的通知屏幕一样工作,然后可以向上滑动它。

I can get my custom view to swipe down, but when I try to swipe it back up, the swipe up gesture is not being initiated. 我可以使自定义视图向下滑动,但是当我尝试将其向上滑动时,不会启动向上滑动手势。

I am a novice, so any help is greatly appreciated! 我是新手,因此非常感谢您的帮助!

- (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);
    }];

}

You should create a property with your notificationView and keep a reference to it, and not create a new one over and over. 您应该使用notificationView创建一个属性并对其进行引用,而不是一遍又一遍地创建一个新属性。

@property (strong, nonatomic) NotificationView *notificationView;

And in your viewDidLoad : 在您的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];

and simply change: 并简单地更改:

-(void)swipeDown:(UISwipeGestureRecognizer *)recognizer {

    [UIView animateWithDuration:2.0 animations:^{
        _notificationView.frame = CGRectMake(0, 723, 414, 723);
    }];

}

You should look into a tutorial on how properties work. 您应该阅读有关属性如何工作的教程。 And you should look into This Thread to avoid animations getting called multiple times etc by handling gesture states. 而且您应该查看此线程,以避免通过处理手势状态而多次调用动画。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM