2017-02-12 70 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意见。

相关问题