gpt4 book ai didi

ios - 让用户画矩形来选择一个区域

转载 作者:搜寻专家 更新时间:2023-10-31 22:14:08 25 4
gpt4 key购买 nike

我是 Swift 的新手,我试图让用户绘制一个矩形(触摸和拖动)来选择图像区域,就像裁剪时一样,但我不想裁剪我只是想知道用户创建的 CGRect。

到目前为止,我有一个 .xib,里面有一个 UIImage 及其 ViewController。我想在图像上方绘图,但我发现的每个有关绘图的教程都是关于子类化 UIView、重写 drawRect 并将其作为 xib 类。

最佳答案

我想通了。我刚刚创建了一个 uiview 并根据触摸事件更改了它的框架

let overlay = UIView()
var lastPoint = CGPointZero

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
overlay.layer.borderColor = UIColor.blackColor().CGColor
overlay.backgroundColor = UIColor.clearColor().colorWithAlphaComponent(0.5)
overlay.hidden = true
self.view.addSubview(overlay)

}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

//Save original tap Point
if let touch = touches.first {
lastPoint = touch.locationInView(self.view)
}
}

override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Get the current known point and redraw
if let touch = touches.first {
let currentPoint = touch.locationInView(view)
reDrawSelectionArea(lastPoint, toPoint: currentPoint)
}
}

func reDrawSelectionArea(fromPoint: CGPoint, toPoint: CGPoint) {
overlay.hidden = false

//Calculate rect from the original point and last known point
let rect = CGRectMake(min(fromPoint.x, toPoint.x),
min(fromPoint.y, toPoint.y),
fabs(fromPoint.x - toPoint.x),
fabs(fromPoint.y - toPoint.y));

overlay.frame = rect
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
overlay.hidden = true

//User has lift his finger, use the rect
applyFilterToSelectedArea(overlay.frame)

overlay.frame = CGRectZero //reset overlay for next tap
}

关于ios - 让用户画矩形来选择一个区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35232985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com