繁体   English   中英

如何正确拖放、旋转和缩放 UIView?

[英]How to drag and drop, rotate and zoom correctly a UIView?

在我的 iOS 应用程序中,我使用 UITouchGestureRecognizer 来拖放、缩放和旋转 UIImageView。 我还将信息存储在 ImageView 的最后一个位置,以便在应用程序重新启动时检索它。

这在 iOS7 上运行良好,但我最近开始在 iOS8 上进行测试,但我遇到了一些问题,尤其是在缩放方面。 每当 imageview 的旋转参数中的角度为非 0 时,缩放不会像预期的那样工作并减小图像的大小。

我不明白为什么操作系统会改变,这是我的代码:

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    [self loadAllImages];
    for (GIFavorite *favorite in self.favorites){
        UIImage *image = favorite.image;
        ImageView *imageView = [[ImageView alloc] initWithFrame:CGRectMake(0, 0, image.size.width, image.size.height)];
        imageView.center = CGPointMake(favorite.x, favorite.y);
        imageView.transform = CGAffineTransformMakeRotation(favorite.rotation);
        imageView.transform = CGAffineTransformScale(imageView.transform, favorite.scale, favorite.scale);
        imageView.image = image;
        imageView.userInteractionEnabled = YES;
        UIPanGestureRecognizer * panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        panRecognizer.delegate = self;
        [imageView addGestureRecognizer:panRecognizer];
        UIRotationGestureRecognizer * rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
        rotationRecognizer.delegate = self;
        [imageView addGestureRecognizer:rotationRecognizer];
        UIPinchGestureRecognizer * pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
        pinchRecognizer.delegate = self;
        [imageView addGestureRecognizer:pinchRecognizer];
        [self.view addSubview:imageView];
    }
}

我想我做变换的方式是错误的,因为当我把CGAffineTransformMakeRotation指令后CGAffineTransformScale ,而不是降低它增加它的大小。

我哪里做错了?

编辑:handleRotate 的代码

- (void)handleRotate:(UIRotationGestureRecognizer *)recognizer {
    recognizer.view.transform = CGAffineTransformRotate(recognizer.view.transform, recognizer.rotation);
    ImageView *imageView = recognizer.view;
    CGFloat radians = atan2f(imageView.transform.b, imageView.transform.a);
    favorite.rotation = radians;
    recognizer.rotation = 0;
}

首先,你需要让你的视图控制器实现 UIGestureRecognizerDelegate。

然后将手势识别器添加到您的 UIView(我看到您已经这样做了,但只需将其再次放在这里即可将解决方案集中在一个地方)。

UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
panGesture.delegate = self;
UIRotationGestureRecognizer *rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
rotationGesture.delegate = self;
UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
pinchGesture.delegate = self;

[yourView addGestureRecognizer:panGesture];
[yourView addGestureRecognizer:rotationGesture];
[yourView addGestureRecognizer:pinchGesture];

然后,这就是你的handle...方法应该是:

- (void)handlePinch:(UIPinchGestureRecognizer *)pinchRecognizer
{
    UIGestureRecognizerState state = [pinchRecognizer state];

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGFloat scale = [pinchRecognizer scale];
        [pinchRecognizer.view setTransform:CGAffineTransformScale(pinchRecognizer.view.transform, scale, scale)];
        [pinchRecognizer setScale:1.0];
    }
}


- (void)handleRotate:(UIRotationGestureRecognizer *)rotationGestureRecognizer {

    UIGestureRecognizerState state = [rotationGestureRecognizer state];

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGFloat rotation = [rotationGestureRecognizer rotation];
        [rotationGestureRecognizer.view setTransform:CGAffineTransformRotate(rotationGestureRecognizer.view.transform, rotation)];
        [rotationGestureRecognizer setRotation:0];
    }
}

- (void)handlePan:(UIPanGestureRecognizer *)panRecognizer {

    UIGestureRecognizerState state = [panRecognizer state];

    if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
    {
        CGPoint translation = [panRecognizer translationInView:panRecognizer.view];
        [panRecognizer.view setTransform:CGAffineTransformTranslate(panRecognizer.view.transform, translation.x, translation.y)];
        [panRecognizer setTranslation:CGPointZero inView:panRecognizer.view];
    }
}

这是它的外观:

在此处输入图片说明

首先,你需要让你的视图控制器实现 UIGestureRecognizerDelegate。

SWIFT 3 :-

func setUpGestures () {

    let panGesture = UIPanGestureRecognizer(target: self, action:#selector(self.handlePanGesture(gesture:)))
    self.imageView.addGestureRecognizer(panGesture)

    let rotationGesture = UIRotationGestureRecognizer(target: self, action:#selector(self.handleRotationGesture(gesture:)))
    self.imageView.addGestureRecognizer(rotationGesture)

    let pinchGesture = UIPinchGestureRecognizer(target: self, action:#selector(self.handlePinchGesture(gesture:)))
    self.imageView.addGestureRecognizer(pinchGesture)
}

func handlePinchGesture(gesture: UIPinchGestureRecognizer) {
    if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
        //print("UIPinchGestureRecognizer")
        gesture.view?.transform = (gesture.view?.transform)!.scaledBy(x: gesture.scale, y: gesture.scale)
        gesture.scale = 1.0
    }
}

func handleRotationGesture(gesture: UIRotationGestureRecognizer) {
    if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
        //print("UIRotationGestureRecognizer")
        gesture.view?.transform = (gesture.view?.transform)!.rotated(by: gesture.rotation)
        gesture.rotation = 0
    }
}

func handlePanGesture(gesture: UIPanGestureRecognizer) {
    if gesture.state == UIGestureRecognizerState.began || gesture.state == UIGestureRecognizerState.changed{
        //print("UIPanGestureRecognizer")
        let translation = gesture.translation(in: view)
        gesture.view?.transform = (gesture.view?.transform)!.translatedBy(x: translation.x, y: translation.y)
        gesture.setTranslation(CGPoint(x: 0, y: 0), in: view)
    }
}

希望,这就是你要找的。 有任何问题都可以回复我。 :)

尝试做这些..

首先在你的 ViewControllers .h 中做这些..

@interface RotationViewController : UIViewController<UIGestureRecognizerDelegate>//Add these..
{
    UIView *testView;
}

现在在您的 ViewController 的 .m 文件中执行这些操作。

- (void)viewDidLoad
{
    [super viewDidLoad];
   // Do any additional setup after loading the view.

    testView=[[UIView alloc]initWithFrame:CGRectMake(100, 100, 160, 160)];

    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 160, 160)];
    [imageView setImage:[UIImage imageNamed:@"ImageName.png"]];
    [imageView setContentMode:UIViewContentModeScaleAspectFit];

    [testView addSubview:imageView];
    [self.view addSubview:testView];

    UIRotationGestureRecognizer *rotationGestureRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];
    [testView addGestureRecognizer:rotationGestureRecognizer];
}

-(void) handleRotate:(UIRotationGestureRecognizer *)rotationGestureRecognizer
{
    testView.transform = CGAffineTransformRotate(testView.transform, rotationGestureRecognizer.rotation);
    rotationGestureRecognizer.rotation = 0.0;
}

我希望它有帮助..

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM