【问题标题】:UISwipeGestureRecognizer with UIView created in a separate class not workingUISwipeGestureRecognizer 与在单独的类中创建的 UIView 不起作用
【发布时间】:2017-02-12 00:29:19
【问题描述】:

我在一个单独的类中创建了一个 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);
    }];

}

【问题讨论】:

    标签: ios objective-c xcode uiview uiswipegesturerecognizer


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      您应该使用您的 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 来避免动画被多次调用等处理手势状态。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-06-24
        • 1970-01-01
        • 2016-08-03
        • 2014-01-30
        • 2015-12-13
        • 2013-10-10
        • 1970-01-01
        相关资源
        最近更新 更多