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:
my output
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
Post a Comment