2017-02-12 25 views
0

我創建了一個UIView在一個單獨的類,我試圖在我的ViewController動畫。它應該像iPhone上的通知屏幕一樣工作,當您向下滑動時出現,然後您可以將其刷回。UISwipeGestureRecognizer與UIView創建在一個單獨的類不工作

我可以讓我的自定義視圖向下滑動,但是當我嘗試將其向後滑動時,向上滑動手勢不會啓動。

我是新手,所以任何幫助是極大的讚賞!

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

} 

回答

0

您應該創建您的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); 
    }]; 

} 

你應該看看在性能如何工作的教程。通過處理姿勢狀態 你應該look into This Thread避免動畫得到多次調用等。

0

notificationView在viewDidLoad中應分配給viewControllers財產,你不應該在頁頭的手勢識別動作的init意見。

相關問題