我在一个单独的类中创建了一个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);
}];
}
答案 0 :(得分: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通过处理手势状态来避免动画被多次调用等。
答案 1 :(得分:0)
ViewDidLoad中的notificationView应分配给viewControllers属性,并且不应在手势识别器操作中分配初始化视图。