adi*_*dit 2 iphone objective-c ipad ios
我不确定为什么当我移动视图时这不会平移?
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));
}
Run Code Online (Sandbox Code Playgroud)
我不清楚你的一些自定义变量等,所以我没有调试你的代码,我想我会告诉你如何使用a来拖动视图UIPanGestureRecognizer.
因此,假设(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;
}
}
Run Code Online (Sandbox Code Playgroud)
虽然上述内容非常简单,但我认为我们可以改善用户体验.值得注意的是,如果用户将视图拖到其超视图之外,上面的代码将不会执行任何操作(例如,拖过左边框并在那里冻结,即使您上下移动手指;也不是非常优雅).我认为以下是一个更优雅的用户界面(但读取代码要复杂一点),当用户拖动他们的手指时,我们会将对象拖动到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;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2697 次 |
| 最近记录: |