In Swift I have a question. I know how to resize views in general using gestures but what I'm trying to do is have the user scale the rectangular view in two different directions. If I'm trying to increase the width of the rectangle I pinch left and right. If I'm trying to increase the length pinch up and down. You get the picture. Here is my code so far. I'm not sure how to convert my pseudo code into real code.
func axisFromPoints(p1: CGPoint, _ p2: CGPoint) -> String {
let x_1 = p1.x
let x_2 = p2.x
let y_1 = p1.y
let y_2 = p2.y
let absolutePoint = CGPoint(x: x_2 - x_1, y: y_2 - y_1)
let radians = atan2(Double(absolutePoint.x), Double(absolutePoint.y))
let absRad = fabs(radians)
if absRad > M_PI_4 && absRad < 3*M_PI_4 {
return "x"
} else {
return "y"
}
}
func handlePinch(_ sender : UIPinchGestureRecognizer) {
//var p1 = location(ofTouch 0,in:sender)
//var p2 = location of touch 1
//call function
// if function == 'x'
//view.transformed.scaledBy(x:sender.scale,y:1)
//if function == 'y'
//view.transformed.scaledBy(x:1,y:sender.scale)
if let view = sender.view {
view.transform = view.transform.scaledBy(x: sender.scale, y: sender.scale)
sender.scale = 1
}
}
Here's How I did it.
func axisFromPoints(p1: CGPoint, _ p2: CGPoint) -> String {
let x_1 = p1.x
let x_2 = p2.x
let y_1 = p1.y
let y_2 = p2.y
let absolutePoint = CGPoint(x: x_2 - x_1, y: y_2 - y_1)
let radians = atan2(Double(absolutePoint.x), Double(absolutePoint.y))
let absRad = fabs(radians)
if absRad > M_PI_4 && absRad < 3*M_PI_4 {
return "x"
} else {
return "y"
}
}
func handlePinch(_ sender : UIPinchGestureRecognizer) {
if sender.state == .changed{
let p1: CGPoint = sender.location(ofTouch: 0, in: view)
let p2: CGPoint = sender.location(ofTouch: 1, in: view)
var x_scale = sender.scale;
var y_scale = sender.scale;
if axisFromPoints(p1,p2) == "x" {
y_scale = 1;
x_scale = sender.scale
}
if axisFromPoints(p1, p2) == "y" {
x_scale = 1;
y_scale = sender.scale
}
view.transform = view.transform.scaledBy(x: x_scale, y: y_scale)
sender.scale = 1
}}