我创建了一个类似Facebook纸张的应用程序,我试图找出如何将视图拖动/捕捉到底部,然后再拖回来。我很确定我完成了75%,但我不能为我的生活弄清楚如何让它突然到底。 这是代码:
- (IBAction)onDrag:(UIPanGestureRecognizer *)sender {
CGPoint translation = [sender translationInView:self.view];
CGPoint center = self.headlinesView.center;
// this is how high the tray should go
int upperThreshold = self.view.frame.size.height - (self.headlinesView.frame.size.height / 2);
// this is how low the tray should go
int lowerThreshold = self.view.frame.size.height + (self.headlinesView.frame.size.height / 2);
if (sender.state == UIGestureRecognizerStateBegan) {
NSLog(@"start");
// reset the tray location to where the tray currently is
self.originalTrayLocation = self.headlinesView.center;
} else if (sender.state == UIGestureRecognizerStateChanged) {
NSLog(@"changed");
if (center.y < upperThreshold) {
// if the tray location is past the point where it should go,
// add some resistance
center.y = upperThreshold + (translation.y * 0.1);
} else if (center.y > lowerThreshold) {
// if the user drags down the tray a lot, don't make the tray
// go that low
center.y = lowerThreshold;
} else {
// drag as normal
center.y = self.originalTrayLocation.y + translation.y;
}
self.headlinesView.center = center;
} else if (sender.state == UIGestureRecognizerStateEnded) {
NSLog(@"ended");
// if the user dragged too far up or down, animate the tray
// back to its desired location
[UIView animateWithDuration:0.3 animations:^{
self.headlinesView.center = CGPointMake(160, upperThreshold);
}];
}
}
任何想法?