gpt4 book ai didi

ios - 为什么平移让用户将缩放 View 移到 super View 之外?

转载 作者:可可西里 更新时间:2023-11-01 05:46:45 27 4
gpt4 key购买 nike

我正在研究 pdf 页面上的收缩和收缩功能。我的捏合和平移(移动)工作正常,但是当用户连续移动缩放 View 时,缩放 View 超出了 super View 边界。是这样的:

theis is the move user made after zoom

我如何限制平移移动,以便用户无法将缩放 View /pdf 移动到 super View 之外。
我使用的相关代码是:

// This method will handle the PINCH / ZOOM gesture 

- (void)pinchZoom:(UIPinchGestureRecognizer *)gestureRecognizer{

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

if (!zoomActive) {
zoomActive = YES;
panActive = YES;
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panMove:)];
[panGesture setMaximumNumberOfTouches:2];
[panGesture setDelegate:self];
[self addGestureRecognizer:panGesture];
[panGesture release];

}

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 - (lastScale - [gestureRecognizer scale]);
newScale = MIN(newScale, kMaxScale / currentScale);
newScale = MAX(newScale, kMinScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;

lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call



[delegate leavesView:self zoomingCurrentView:[gestureRecognizer scale]];




}


我处理平移移动的方法:

// This method will handle the PAN / MOVE gesture 
- (void)panMove:(UIPanGestureRecognizer *)gestureRecognizer
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[[gestureRecognizer view] superview]];
[[gestureRecognizer view] setCenter:CGPointMake([[gestureRecognizer view] center].x + translation.x, [[gestureRecognizer view] center].y + translation.y)];

[gestureRecognizer setTranslation:CGPointZero inView:[[gestureRecognizer view] superview]];
}
}

请建议如何在其父 View 范围内处理平移/移动限制平移。

最佳答案

在你的 panMove 方法中尝试这段代码。它在我的情况下工作正常。

        static CGPoint initialCenter;

if (recognizer.state == UIGestureRecognizerStateBegan)
{
initialCenter = recognizer.view.center;
}

CGPoint translation = [recognizer translationInView:recognizer.view];
CGPoint newCenter = CGPointMake(initialCenter.x + translation.x,
initialCenter.y + translation.y);
CGRect newFrame = recognizer.view.frame;
CGRect superViewBounds = recognizer.view.superview.bounds;
CGPoint superViewOrigin = recognizer.view.superview.frame.origin;

if(newCenter.x-(newFrame.size.width/2) >= (superViewBounds.size.width+superViewOrigin.x)-200 /*right*/
||
newCenter.x+(newFrame.size.width/2) <= (superViewOrigin.x+200) /*left*/
||
newCenter.y-(newFrame.size.height/2) >= (superViewBounds.size.height+superViewOrigin.y)-200 /*bottom*/
||
newCenter.y+(newFrame.size.height/2) <= (superViewOrigin.y+100)) /*top*/
{
return;
}else{
recognizer.view.center = newCenter;
}

关于ios - 为什么平移让用户将缩放 View 移到 super View 之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10914679/

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