ios - Rotate ImageView using CGAffineTransform and PanGestureRecognizer -


i'm trying rotate imageview have using cgaffinetransform. basically, want happen every time x-coordinate increases, want imageview rotate little more.

i did math , want imageview rotate 1.6º every 1 x-coordinate. have far in gesture recognizer function:

@objc func persondragrecognizer(recognizer: uipangesturerecognizer) {      let rotationangle: cgfloat = 1.6      let translation = recognizer.translation(in: rview)     if let view = recognizer.view {         view.center = cgpoint(x:view.center.x +  translation.x, y:view.center.y + translation.y)         view.transform = cgaffinetransform(rotationangle: rotationangle)     }     recognizer.settranslation(cgpoint.zero, in: rview)      if recognizer.view?.center == cgpoint(x: 197.5, y: 232.5)  {         playgroundpage.current.liveview = eeview     } 

the problem rotates 576º bc that's 1.6 times 360. , doesn't keep on rotating once. want continually rotate.

if help immensely appreciated. so in advance!!

cheers, theo

i'm not sure if understood feature you're trying implement. here sample uiview subclass responsible image rotation.

class customview: uiview {     override init(frame: cgrect) {         super.init(frame: frame)          let pangr = uipangesturerecognizer(target: self, action: #selector(pangesturedetected(sender:)))         self.gesturerecognizers = [pangr]     }      required init?(coder adecoder: nscoder) {         fatalerror("init(coder:) has not been implemented")     }      var gesturebeginning: cgfloat = 0.0      func pangesturedetected(sender: uipangesturerecognizer) {         guard sender.numberoftouches > 0 else { return }          let touchpoint = sender.location(oftouch: 0, in: self)          switch sender.state {         case .began:             gesturebeginning = touchpoint.x             print("gesturebeginning: \(gesturebeginning)")         case .changed:             let progress = touchpoint.x - gesturebeginning             print("progress: \(progress)")             let rotationangle: cgfloat = 1.6 * progress             let view = self.subviews[0]             let rads =  rotationangle * .pi / 180             view.transform = cgaffinetransform(rotationangle: rads)         default:             break         }     } } 

to run on playground:

let imageview = uiimageview(image: #imageliteral(resourcename: "image.png")) imageview.frame = cgrect(origin: cgpoint(x: 50.0, y: 50.0), size: cgsize(width: 150.0, height: 150.0))  let containerview = customview(frame: cgrect(origin: .zero, size: cgsize(width: 250.0, height: 250.0))) containerview.addsubview(imageview)  playgroundpage.current.liveview = containerview playgroundpage.current.needsindefiniteexecution = true 

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -