gpt4 book ai didi

ios - 使用 UIPinchGestureRecognizer 捏合时限制 UIPanGestureRecognizer,适用于 iPad

转载 作者:行者123 更新时间:2023-12-01 16:42:20 27 4
gpt4 key购买 nike

我正在尝试在 iPad View 上同时创建典型的捏缩放和平移。 View 本身包含 4 个其他 subview 。
我遇到的问题是,虽然我可以同时捏缩放和平移,但平移超出了范围,以至于您可以将 View 移出框架。

我尝试了几个平移限制,但它们似乎都只限制 Y 轴,或者同时限制 X 和 Y,但仅限于未放大的情况。

有什么建议么?

这是我用于捏缩放和平移手势的内容:

// for Pan Gesture
- (void) handlePanGestures:(UIPanGestureRecognizer *)recognizer
{
if (recognizer.state != UIGestureRecognizerStateEnded && recognizer.state != UIGestureRecognizerStateFailed)
{
CGPoint translation = [recognizer translationInView:self.view];
CGPoint finalPoint = CGPointMake(recognizer.view.center.x + translation.x, recognizer.view.center.y + translation.y);

CGPoint location = [recognizer locationInView:recognizer.view.superview];
recognizer.view.center = location;

// set final position
recognizer.view.center = finalPoint;
[recognizer setTranslation:CGPointZero inView:self.view];
}
}

// for Pinch Gesture
- (void)handlePinches:(UIPinchGestureRecognizer *)gestureRecognizer
{

if([gestureRecognizer state] == UIGestureRecognizerStateBegan)
{
// Reset the last scale, necessary if there are multiple objects with different scales
self.currentScale = [gestureRecognizer scale];
}

if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged)
{

CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];

// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;

CGFloat newScale = 1 - (self.currentScale - [gestureRecognizer scale]);
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;

self.currentScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}

最佳答案

试试这个,对我有用!

-(void)moveViewWithGestureRecognizer:(UIPanGestureRecognizer *)recognizer{

CGRect xRectRef = self.imageView.superview.frame;
CGPoint translation = [recognizer translationInView:self.imageView];

CGPoint xyNew = CGPointMake(self.imageView.frame.origin.x + translation.x, self.imageView.frame.origin.y + translation.y);

if (recognizer.state == UIGestureRecognizerStateChanged ) {

if ( (xyNew.y <= xRectRef.origin.y) &&
(xyNew.y + self.imageView.frame.size.height >= xRectRef.origin.y + xRectRef.size.height) &&
(xyNew.x <= xRectRef.origin.x ) &&
(xyNew.x + self.imageView.frame.size.width >= xRectRef.size.width+xRectRef.origin.x)){
self.imageView.center = CGPointMake(self.imageView.center.x + translation.x,
self.imageView.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.imageView];
}
}

}

关于ios - 使用 UIPinchGestureRecognizer 捏合时限制 UIPanGestureRecognizer,适用于 iPad,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23158432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com