缩放、旋转和拖动 UIImageView

Posted

技术标签:

【中文标题】缩放、旋转和拖动 UIImageView【英文标题】:Scale, rotate and drag UIImageView 【发布时间】:2011-04-15 13:55:15 【问题描述】:
-(void)scale:(id)sender 

[self.view bringSubviewToFront:[(UIPinchGestureRecognizer*)sender view]];

if([(UIPinchGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) 

    lastScale = 1.0;
    return;


CGFloat scale = 1.0 - (lastScale - [(UIPinchGestureRecognizer*)sender scale]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

[[(UIPinchGestureRecognizer*)sender view] setTransform:newTransform];

lastScale = [(UIPinchGestureRecognizer*)sender scale];


-(void)rotate:(id)sender 

[self.view bringSubviewToFront:[(UIRotationGestureRecognizer*)sender view]];

if([(UIRotationGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) 

    lastRotation = 0.0;
    return;


CGFloat rotation = 0.0 - (lastRotation - [(UIRotationGestureRecognizer*)sender rotation]);

CGAffineTransform currentTransform = [(UIPinchGestureRecognizer*)sender view].transform;
CGAffineTransform newTransform = CGAffineTransformRotate(currentTransform,rotation);

[[(UIRotationGestureRecognizer*)sender view] setTransform:newTransform];

lastRotation = [(UIRotationGestureRecognizer*)sender rotation];


-(void)move:(id)sender 

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];

[self.view bringSubviewToFront:[(UIPanGestureRecognizer*)sender view]];
CGPoint translatedPoint = [(UIPanGestureRecognizer*)sender translationInView:self.view];

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateBegan) 

    firstX = [[sender view] center].x;
    firstY = [[sender view] center].y;


translatedPoint = CGPointMake(firstX+translatedPoint.x, firstY+translatedPoint.y);

[[sender view] setCenter:translatedPoint];

if([(UIPanGestureRecognizer*)sender state] == UIGestureRecognizerStateEnded) 

    CGFloat finalX = translatedPoint.x + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].x);
    CGFloat finalY = translatedPoint.y + (.35*[(UIPanGestureRecognizer*)sender velocityInView:self.view].y);

    if(UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) 

        if(finalX < 0)                                 finalX = 0;                                    else if(finalX > 768) 

            finalX = 768;
        

        if(finalY < 0)                                 finalY = 0;                                    else if(finalY > 1024) 

            finalY = 1024;
        
    

    else 

        if(finalX < 0)                                 finalX = 0;                                    else if(finalX > 1024) 

            finalX = 768;
        

        if(finalY < 0)                                 finalY = 0;                                    else if(finalY > 768) 

            finalY = 1024;
        
    

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:.35];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    [[sender view] setCenter:CGPointMake(finalX, finalY)];
    [UIView commitAnimations];



-(void)tapped:(id)sender 

[[[(UITapGestureRecognizer*)sender view] layer] removeAllAnimations];


- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer      shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 

return ![gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];

这是我的代码,我要移动的 UIImageView 称为“Birdie”。我知道我必须创建一个 -(void)Birdie UIImageView 识别器,但我使用了以下没有用的方法。

    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[Birdie addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[Birdie addGestureRecognizer:rotationRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[Birdie addGestureRecognizer:panRecognizer];

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[Birdie addGestureRecognizer:tapRecognizer];

[self.view addSubview:Birdie];

非常感谢!

【问题讨论】:

我推荐你回答这篇文章[在此处输入链接描述][1] [1]:***.com/questions/5822214/…希望它有所帮助:) 我推荐你回答这篇文章[拖动、缩放和旋转单个或多个 UIImageviews][1] [1]:***.com/questions/5822214/… :) 【参考方案1】:

你需要在界面中放入一些值,比如:

//Zoom
CGFloat lastScaleFactor;

//Movement
CGPoint startLocation;
float distance;

//Rotation
CGFloat valore;

然后:

- (void)zoomAction:(UIGestureRecognizer *)sender 

    CGFloat factor = [(UIPinchGestureRecognizer *)sender scale];
    if (factor > 1) 

        self.yourImage.transform = CGAffineTransformMakeScale(lastScaleFactor + (factor-1), lastScaleFactor + (factor-1));

     else 

        self.yourImage.transform = CGAffineTransformMakeScale(lastScaleFactor * factor, lastScaleFactor * factor);

    

    //Save the new "scale" factor
    if (sender.state == UIGestureRecognizerStateEnded)  
        if (factor > 1) 
            lastScaleFactor += (factor-1); 
         else 
            lastScaleFactor *= factor;
        
    

- (void)panAction:(UIPanGestureRecognizer *)gesture 
    if ((gesture.state == UIGestureRecognizerStateChanged) || (gesture.state == UIGestureRecognizerStateEnded)) 

        CGPoint location = [gesture locationInView:self.view];

        [self.yourImage setCenter:location];
    


- (void)rotationAction:(UIRotationGestureRecognizer *)gesture 
    if (gesture.state == UIGestureRecognizerStateBegan) 
         [gesture setRotation:valore * 10];
    
    if (gesture.state == UIGestureRecognizerStateChanged) 
        valore = gesture.rotation / 10;
        if (valore > 1.0) valore = 1.0;
        if (valore < 0.2) valore = 0.2;

        self.yourImage.alpha = valore;
    
    NSLog(@"rotazione %f", gesture.rotation / 10);

在这种情况下,我使用旋转来更改 alpha,但你可以做你需要的

【讨论】:

以上是关于缩放、旋转和拖动 UIImageView的主要内容,如果未能解决你的问题,请参考以下文章

如何在Android中对图像应用缩放、拖动和旋转[关闭]

touch.js 拖动缩放旋转 (鼠标手势)

touch.js 拖动缩放旋转 (鼠标手势)

Cesium.js禁止三维球鼠标旋转、缩放、鼠标中键拖动

Blender网格编辑工具之移动,旋转,缩放

如何使用ZBrush中的位移、旋转和缩放功能