在 iOS 5 中向下拖动 UIView



我在iPhone应用程序中看到状态栏上有一个手势可以访问通知中心。如何在我的应用中实现这种转换?我认为这是通过滑动手势识别器完成的,但是如何从上到下包含滑动手势(如何拖动通知中心完成其完整过渡)?是否有任何示例代码或可以帮助我做到这一点的东西?提前他

应该很容易做到。假设您有一个UIViewmainView),您要从中触发下拉。

  1. 在可见区域外的主视图顶部放置一个子视图(pulldownView)。
  2. mainView上实现touchesBegan,并检查触摸是否位于前 30 个像素(或点)中。
  3. 在检查的位置实现touchesMoved,如果移动方向向下且pulldownView不可见,如果是,则将pulldownView向下拖动到主视图的可见区域,或者检查移动方向是否向上且pulldownView可见,如果是,则向上推出可见区域。
  4. 通过检查pulldownView的移动方向,在结束拖动或推动移动的位置实现touchesEnd

编辑:

下面是一些示例代码。未经测试,可能包含拼写错误,可能无法编译,但应包含所需的基本部分。

//... inside mainView impl:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = (UITouch *)[touches anyObject];
  start = [touch locationInView:self.superview].y;
  if(start > 30 && pulldownView.center.y < 0)//touch was not in upper area of view AND pulldownView not visible
  {
    start = -1; //start is a CGFloat member of this view
  }
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  if(start < 0)
  {
    return;
  }
  UITouch *touch = (UITouch *)[touches anyObject];
  CGFloat now = [touch locationInView:self.superview].y;
  CGFloat diff = now - start;
  directionUp = diff < 0;//directionUp is a BOOL member of this view
  float nuCenterY = pulldownView.center.y + diff;
  pulldownView.center = CGPointMake(pulldownView.center.x, nuCenterY);
  start = now;
}

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  if (directionUp)
  {
    //animate pulldownView out of visibel area
    [UIView animateWithDuration:.3 animations:^{pulldownView.center = CGPointMake(pulldownView.center.x, -roundf(pulldownView.bounds.size.height/2.));}];
  }
  else if(start>=0)
  {
    //animate pulldownView with top to mainviews top
    [UIView animateWithDuration:.3 animations:^{pulldownView.center = CGPointMake(pulldownView.center.x, roundf(pulldownView.bounds.size.height/2.));}];
  }
}

你可以实现 UISwipeGestureRecognizer 来处理应用中的轻扫手势,并指定要检测为 UISwipeGestureRecognizerDirectionDown 的轻扫方向。

这里有一个苹果样本。还有一些WWDC10会议值得一看。我建议从该页面"使用手势识别器简化触摸事件处理"和"高级手势识别"。

我通过在全尺寸UIScrollView中使用子类化UIView来解决此问题。如果用户触摸到非"拖动"的点,UIView会将触摸事件传递到其底层视图。

@implementation MyCustomUIView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    // Check whether we are on the overlay
    if
    (
        point.y < 460.0f
        // Maybe some more restrictions here
    )
    {
        // Block the event, pass to parent
        return NO;
    }
    // Everything okay
    return YES;
}
@end

此代码使UIScrollView仅响应 460 像素以下的触摸。例如,如果您的UIScrollView高度为 800 像素,则可以从 460 像素触摸它到 800 像素。足以打开,关闭和使用它。因此,如果您有 全尺寸UIScrollView ,您可以向上拖动它以打开它,同时您可以"触摸"底层视图。此方法的好处是所有动画都中继默认行为,您不必自己实现它。

最新更新