在iOS 5中向下拖动UIView

sta*_*one 4 iphone cocoa-touch objective-c uigesturerecognizer ios5

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

Kai*_*ann 11

应该很容易做到.假设你有一个UIView(mainView),你想要触发下拉的东西.

  1. pulldownView在可见区域顶部的mainView上放置一个subview().
  2. 实施touchesBeganmainView,并检查触摸是在顶部30个像素(或点).
  3. 实施touchesMoved检查的位置,如果移动方向向下并且pulldownView不可见,如果是这样,将其pulldownView向下拖动到主视图的可见区域或检查移动方向是否向上并且pulldownView可见,如果是这样,则向上推出可见区域.
  4. touchesEnd通过检查移动方向来实现拖动或推动移动的位置pulldownView.

编辑:

这是一些示例代码.未经测试,可能包含拼写错误,可能无法编译,但应包含所需的必要部分.

//... 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.));}];
  }
}
Run Code Online (Sandbox Code Playgroud)