我不确定为什么在移动视图时这不会平移?
CGPoint translation = [recognizer translationInView:self.view];
if ((recognizer.view.frameY + translation.y >= self.view.frameHeight - recognizer.view.frameHeight)) {
CGPoint newOrigin = CGPointMake(self.view.bounds.size.width, roundf(recognizer.view.frameY + translation.y));
recognizer.view.frameOrigin = newOrigin;
NSLog(@"FRAME ORIGIN %@", NSStringFromCGPoint(recognizer.view.frameOrigin));
}
答案 0 :(得分:6)
我不清楚你的一些自定义变量等,所以我没有调试你的代码,我想我会告诉你如何使用UIPanGestureRecognizer
来拖动视图。< / p>
因此,假设(a)手势已被添加到被拖动的视图中; (b)该视图包含在您想要限制视图移动的超级视图中,代码可能如下所示:
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
static CGRect originalFrame;
if (gesture.state == UIGestureRecognizerStateBegan)
{
originalFrame = gesture.view.frame;
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
CGPoint translate = [gesture translationInView:gesture.view.superview];
CGRect newFrame = CGRectMake(originalFrame.origin.x + translate.x,
originalFrame.origin.y + translate.y,
originalFrame.size.width,
originalFrame.size.height);
if (CGRectContainsRect(gesture.view.superview.bounds, newFrame))
gesture.view.frame = newFrame;
}
}
虽然上述内容非常简单,但我认为我们可以改善用户体验。值得注意的是,如果用户将视图拖到其超视图之外,上面的代码将不会执行任何操作(例如,拖过左边框并在那里冻结,即使您上下移动手指;也不是非常优雅)。我认为以下是一个更优雅的用户界面(但读取代码要复杂一点),当用户拖动他们的手指时,我们会将对象拖动到superview中最接近的点,对应于用户的手指是。
- (void)handlePan:(UIPanGestureRecognizer *)gesture
{
static CGRect originalFrame;
if (gesture.state == UIGestureRecognizerStateBegan)
{
originalFrame = gesture.view.frame;
}
else if (gesture.state == UIGestureRecognizerStateChanged)
{
CGPoint translate = [gesture translationInView:gesture.view.superview];
CGRect newFrame = CGRectMake(fmin(gesture.view.superview.frame.size.width - originalFrame.size.width, fmax(originalFrame.origin.x + translate.x, 0.0)),
fmin(gesture.view.superview.frame.size.height - originalFrame.size.height, fmax(originalFrame.origin.y + translate.y, 0.0)),
originalFrame.size.width,
originalFrame.size.height);
gesture.view.frame = newFrame;
}
}
答案 1 :(得分:1)
泛手势法叫做了吗?如果未设置启用用户交互。你可以使用下面的代码。
- (void)handlePan:(UIPanGestureRecognizer *)recognizer {
CGPoint translation = [recognizer translationInView:recognizer.view.superview];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:recognizer.view.superview];
if (recognizer.state == UIGestureRecognizerStateEnded) {
CGPoint velocity = [recognizer velocityInView:recognizer.view.superview];
CGFloat magnitude = sqrtf((velocity.x * velocity.x) + (velocity.y * velocity.y));
CGFloat slideMult = magnitude / 200;
NSLog(@"magnitude: %f, slideMult: %f", magnitude, slideMult);
float slideFactor = 0.1 * slideMult; // Increase for more of a slide
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + (velocity.x * slideFactor),
recognizer.view.center.y + (velocity.y * slideFactor));
finalPoint.x = MIN(MAX(finalPoint.x, 0), recognizer.view.superview.bounds.size.width);
finalPoint.y = MIN(MAX(finalPoint.y, 0), recognizer.view.superview.bounds.size.height);
[UIView animateWithDuration:slideFactor*2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
recognizer.view.center = finalPoint;
} completion:nil];
}
}
答案 2 :(得分:0)
在if
中,您应该写<=
而不是>=
,因为原点位于左上角