Moving UIView within UIImageView's UIImage in Swift -


i trying move 1 image on base image.

if moving uiview, crosses uiimageview's image.

how restrict uiview's move within uiimageview's image? should supports square , rectangle. moving view should not cross border.

kindly guide me.

in screen shots, uiimageview background color black color. moving uiview in white color. white color view crossing image [screen shot 2]. how restrict within image ?

code:

 @ibaction func panacn(sender: uipangesturerecognizer) {      if sender.state == .began || sender.state == .changed {          let translation = sender.translationinview(self.myimgvw)         // note: 'view' optional , need unwrapped         sender.view!.center = cgpointmake(sender.view!.center.x + translation.x, sender.view!.center.y + translation.y)         sender.settranslation(cgpointmake(0,0), inview: self.myimgvw)     }  } 

need output:

enter image description here

my output

enter image description here

you may try :

 @ibaction func panacn(sender: uipangesturerecognizer) {      if sender.state == .began || sender.state == .changed {          let translation = sender.translationinview(self.myimgvw)         // note: 'view' optional , need unwrapped         guard let view = sender.view else { return }         var newframe = view.frame         newframe.origin.x = max(self.myimgvw.frame.origin.x, view.frame.origin.x + translation.y)           newframe.origin.y = max(self.myimgvw.frame.origin.y, view.frame.origin.y + translation.y)         view.frame = newframe             }  } 

Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -