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. 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.

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.

  • How to integrate both Objective-C and Swift pods in same project in iOS app
  • UITableView scroll smooth with certain speed?
  • iOS5 unable to read symbols for Riven.bundle (file not found)
  • NSTextField margin and padding? (Swift)
  • What does C “Sleep” function (capital “S”) do on a Mac?
  • Stray /377 in xcode
  • 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);
        }];
    
    }
    

    2 Solutions Collect From Internet About “UISwipeGestureRecognizer with UIView created in a separate class not working”

    notificationView in ViewDidLoad should be assigned to the viewControllers property, and you should not alloc init views in the gesture recognizer actions.

    You should create a property with your notificationView and keep a reference to it, and not create a new one over and over.

    @property (strong, nonatomic) NotificationView *notificationView;
    

    And in your 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.