UIView像iOS控制中心一样弹跳

时间:2015-04-19 02:57:50

标签: ios objective-c xcode uiview uidynamicanimator

我试图创建一个类似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;
    }
}
}

演示: My Program iOS Control Centre

问题1: 任何人都可以举一些例子或指导我如何实现这一目标?

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以使用带弹簧的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