【问题标题】:UIView Bouncing like iOS Control CentreUIView 像 iOS 控制中心一样弹跳
【发布时间】:2015-06-25 20:16:27
【问题描述】:

我正在尝试制作一个具有类似 iOS 控制中心行为的 UIView。我当前的代码能够上拉显示和下拉隐藏。

#define kViewMaxHeight 600
#define kViewMinHeight 300
#define kViewWidth [[UIScreen mainScreen] bounds].size.width
#define kViewDefaultX 0
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height

- (void)swipeView:(id)sender {
CGPoint location = [(UIPanGestureRecognizer *)sender locationInView:self];
CGPoint translatedPoint = [(UIPanGestureRecognizer *)sender translationInView:self];
CGPoint velocity = [(UIPanGestureRecognizer *)sender velocityInView:self];
NSLog(@"x:%.2f, y:%.2f", location.x, location.y);
if([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateBegan) {

    firstX = [self center].x;
    firstY = [self center].y;
}

translatedPoint = CGPointMake(firstX, firstY+translatedPoint.y);

// Dragged Up
if (velocity.y < 0) {
    if (self.frame.origin.y + self.frame.size.height > kScreenHeight) {
        [self setCenter:translatedPoint];
    }
    else {

    }
}
// Dragged Down
else {
    [self setCenter:translatedPoint];
}

//
if (self.frame.origin.y + self.frame.size.height < kScreenHeight) {
    CGRect frame = self.frame;
    frame.origin.y = kScreenHeight - kViewMinHeight;
    self.frame = frame;
}

if ([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateEnded) {

    if (velocity.y < 0 || location.y < 0) {
        CGRect frame = self.frame;
        frame.origin.y = kScreenHeight - kViewMinHeight;
        self.frame = frame;
    }
    else {
        CGRect frame = self.frame;
        frame.origin.y = kScreenHeight - 50;
        frame.size.height = kViewMinHeight;
        self.frame = frame;
    }
}
}

演示:

问题 1: 谁能举一些例子或指导我如何做到这一点?

问题 2(这里有一些愚蠢的问题): iOS 控制中心是如何实现的?是使用 UIViewController 还是 UIView?

【问题讨论】:

  • 控制中心很可能既不是 UIView 也不是 UIViewController,它可能只是硬编码到 iOS 中

标签: ios objective-c xcode uiview uidynamicanimator


【解决方案1】:

您可以通过使用带有弹簧的 UIView 动画来实现移动。调整持续时间、阻尼比、速度的值并增加 center.y,直到找到正确的组合。这对你来说应该是一个好的开始:

// sliding up
    UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10.0, options: .curveEaseOut, animations: {
    // Assuming self is the view
    self.center.y += 50
    }

// sliding down, subtract the value for center.y
    UIView.animate(withDuration: 0.5, delay: 0.0, usingSpringWithDamping: 0.5, initialSpringVelocity: 10.0, options: .curveEaseOut, animations: {
    // Assuming self is the view
    self.center.y -= 50
    }

额外的调整,看看我在这里创建的组件: https://github.com/narumolp/NMPAnchorOverlayView

【讨论】:

    【解决方案2】:

    尝试使用 UIGravityBehavior 和 UICollisionBehavior。当用户拉起面板时,将其添加到碰撞行为中,当用户拉下面板时,关闭碰撞行为。

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 1970-01-01
      • 2015-08-18
      • 2017-06-24
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多