1

我正在尝试制作一个具有类似 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;
    }
}
}

演示: 我的程序 iOS 控制中心

问题1: 任何人都可以举一些例子或指导我如何做到这一点?

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

4

2 回答 2

0

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

于 2015-06-24T21:07:14.027 回答
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

于 2017-05-30T21:54:37.617 回答