当前位置:  开发笔记 > 运维 > 正文

Swift:如何使用顶点的四个按钮实现可调整大小的个性化uiview

如何解决《Swift:如何使用顶点的四个按钮实现可调整大小的个性化uiview》经验,为你挑选了0个好方法。

我想实现一个可调整大小和可拖动的UIView,如下图所示:

在此输入图像描述

这是我到目前为止使用简单的手势识别器实现的类:

class PincherView: UIView {

var pinchRec:UIPinchGestureRecognizer!
var rotateRec:UIRotationGestureRecognizer!
var panRec:UIPanGestureRecognizer!
var containerView:UIView!

init(size: CGRect, container:UIView) {
    super.init(frame: size)

    self.containerView = container

    self.pinchRec = UIPinchGestureRecognizer(target: self, action: "handlePinch:")
    self.addGestureRecognizer(self.pinchRec)

    self.rotateRec = UIRotationGestureRecognizer(target: self, action: "handleRotate:")
    self.addGestureRecognizer(self.rotateRec)

    self.panRec = UIPanGestureRecognizer(target: self, action: "handlePan:")
    self.addGestureRecognizer(self.panRec)

    self.backgroundColor = UIColor(red: 0.0, green: 0.6, blue: 1.0, alpha: 0.4)
    self.userInteractionEnabled = true
    self.multipleTouchEnabled = true
    self.hidden = false
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func handlePinch(recognizer : UIPinchGestureRecognizer) {
    if let view = recognizer.view {
        view.transform = CGAffineTransformScale(view.transform, 1.0, recognizer.scale)
        //view.transform = CGAffineTransformScale(view.transform, recognizer.scale, recognizer.scale)
        recognizer.scale = 1
    }
}

func handleRotate(recognizer : UIRotationGestureRecognizer) {
    if let view = recognizer.view {
        view.transform = CGAffineTransformRotate(view.transform, recognizer.rotation)
        recognizer.rotation = 0
    }
}

func handlePan(recognizer:UIPanGestureRecognizer) {
    let translation = recognizer.translationInView(containerView)
    if let view = recognizer.view {
        view.center = CGPoint(x:view.center.x + translation.x,
            y:view.center.y + translation.y)
    }
    recognizer.setTranslation(CGPointZero, inView: containerView)
}

}

正如您可以想象的那样,简单地说UIPinchGestureRecognizer,uiview在x和y方向上的比例相同,但我希望用户有机会尽可能准确地进行调整,以便他们可以在一个方向上进行缩放或改变形式uiview,不一定是矩形.此外,对我来说更重要的是,用户在设置uiview 的高度时尽可能精确.这就是为什么我需要在角落处设置这4个按钮的原因.我希望你能给我一些从哪里开始以及如何开始的提示.谢谢

更新 这是我所知道的

在此输入图像描述

通过这种方式,我有以下层次结构:

- UIImageView嵌入 - UIView嵌入 - UIScrollView嵌入 -UIViewController

我不得UIView不再添加一个用作容器,因此它包含的所有子视图都将与图片一起放大/缩小,以便更精确地将天蓝色uiview与拍摄对象更好地重叠.关于针,小白圈,这是我试图实现的类知道:

class PinImageView: UIImageView {

var lastLocation:CGPoint!
var container:UIView!
var viewToScale:UIView!
var id:String!

init(imageIcon: UIImage?, location:CGPoint, container:UIView, viewToScale:UIView, id:String) {

    super.init(image: imageIcon)
    self.lastLocation = location

    self.frame = CGRect(x: location.x, y: location.y, width: 10.0, height: 10.0)
    self.center = location

    self.userInteractionEnabled = true

    self.container = container
    self.viewToScale = viewToScale
    self.id = id
    self.hidden = false
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func getScaleParameters(arrivalPoint:CGPoint) -> (sx:CGFloat, sy:CGFloat) {

    //var result:(sx:CGFloat, sy:CGFloat) = (1.0, 1.0)
    var scaleX:CGFloat = 1.0
    var scaleY:CGFloat = 1.0

    switch (self.id) {
    case "up_left":
        /* up_left is moving towards right*/
        if (self.lastLocation.x < arrivalPoint.x) {
            scaleX = -0.99
        }
        /* up_left is moving towards left*/
        else {
            scaleX = 0.9
        }
        /* up_left is moving towards up*/
        if (self.lastLocation.y < arrivalPoint.y) {
            scaleY = -0.9
        }
        /* up_left is moving towards down*/
        else {
            scaleY = 0.9
        }
        break
    default:
        scaleX = 1.0
        scaleY = 1.0
    }

    return (scaleX, scaleY)

}

override func touchesMoved(touches: Set, withEvent event: UIEvent?) {
    if let touch: UITouch = touches.first {
        let scaleParameters:(sx:CGFloat, sy:CGFloat) = self.getScaleParameters(touch.locationInView(self.container))
        self.viewToScale.transform = CGAffineTransformMakeScale(scaleParameters.sx, scaleParameters.sy)
        //self.center = touch.locationInView(self.container)
        self.center = touch.locationInView(self.container)
        self.lastLocation = touch.locationInView(self.container)
    }
}

}

viewToScale蔚蓝的浮动uiview 在哪里叫pincherViewRec.开头的引脚中心位于pincherViewRec.frame.origin.说它不能按预期工作是没用的.你有什么主意吗?任何暗示都会受到赞赏,以帮助我找到正确的方法.

推荐阅读
手机用户2502852037
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有