• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-13 10:48:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

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

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

-(void) viewDidAppearBOOL)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 actionselector(handlePan];
        panRecognizer.delegate = self;
        [imageView addGestureRecognizer:panRecognizer];
        UIRotationGestureRecognizer * rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self actionselector(handleRotate];
        rotationRecognizer.delegate = self;
        [imageView addGestureRecognizer:rotationRecognizer];
        UIPinchGestureRecognizer * pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self actionselector(handlePinch];
        pinchRecognizer.delegate = self;
        [imageView addGestureRecognizer:pinchRecognizer];
        [self.view addSubview:imageView];
    }
}

我猜我进行转换的方式是错误的,因为当我将 CGAffineTransformMakeRotation 指令放在 CGAffineTransformScale 之后而不是减小大小时,它会增加它。

我哪里做错了?

编辑:handleRotate 的代码

- (void)handleRotateUIRotationGestureRecognizer *)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;
}



Best Answer-推荐答案


首先,你需要让你的 View Controller 实现 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)
    }
}

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

关于ios - 如何正确拖放、旋转和缩放 UIView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32538629/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap