Ios 如何通过PangestureRecognitor控制imageView的移动?
我想通过PangestureRecognitor这样移动imageView imageView可以缩放 如果imageView的位置为0,0,则它无法移动。 如果imageView的位置x超过60,则无法再移动。 如果imageView的位置y超过80,则不能再移动了。 当imageView的比例恢复为1.0时,其位置为0,0。 很难限制imageView的移动和位置。 我该怎么办 这是我的密码Ios 如何通过PangestureRecognitor控制imageView的移动?,ios,objective-c,uipangesturerecognizer,Ios,Objective C,Uipangesturerecognizer,我想通过PangestureRecognitor这样移动imageView imageView可以缩放 如果imageView的位置为0,0,则它无法移动。 如果imageView的位置x超过60,则无法再移动。 如果imageView的位置y超过80,则不能再移动了。 当imageView的比例恢复为1.0时,其位置为0,0。 很难限制imageView的移动和位置。 我该怎么办 这是我的密码 img = [UIImage imageNamed:[NSString stringWithForm
img = [UIImage imageNamed:[NSString stringWithFormat:@"a.jpg"]];
imgView = [[UIImageView alloc]initWithImage:img];
imgView.frame = CGRectMake(0,0, self.view.frame.size.width, 448);
imgView.userInteractionEnabled = YES;
[self.view imgView];
- (void)panAction : (UIPanGestureRecognizer *)sender {
CGPoint CGP = imgView.center;
if(newScale != 1.0 && CGP.x-160 != 0 && CGP.y-224 != 50){
CGPoint p = [sender translationInView:self.view];
CGPoint movedPoint = CGPointMake(imgView.center.x + p.x, imgView.center.y + p.y);
imgView.center = movedPoint;
[sender setTranslation:CGPointZero inView:self.view];
}
}
-(void)handlePinchGesture:(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) {
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;
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
}
}
在平移手势动作中必须使用以下条件检查: 为您提供信息,在移动对象时,每次像素移动时都会定期调用此panAction方法
- (void)panAction : (UIPanGestureRecognizer *)sender {
if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan)
{
//Do something whatever you need to be done while the pan gesture starts
}
if ([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateChanged)
{
//Do something whatever you need to be done while the imageView object is in the moving state
}
if ([(UIPanGestureRecognizer *)sender state] == UIGestureRecognizerStateEnded){
//Do something you need to do as you end the pan gesture.
}
}