I am attempting to implement a user-used photo crop tool and am stuck. I have a UIView (imageCropper) that is on top (z-order-wise) of the UIImageView (imageToBeEdited) which contains the image that is being cropped. If I run the app at this point, with my other configurations and logic, imageCropper is right on top of the image. However, when I set the maskView of imageToBeEdited to imageCropper, the origin of imageCropper shifts. After messing with the constraint constants, I determined that it is shifting to a greater x by the exact value of the leading constraint and a greater y by the exact value of the top constraint.
Another piece of info that makes this even more confusing, is that I have another UIView (imageCropperBorder) on top (again z-order-wise) of imageCropper that acts simply as a visible border. I set it's frame to the exact same frame as imageCropper and yet it is where they are both supposed to be.
I have no constraints set on imageCropper or imageCropperBorder.
My questions are why is imageCropper shifting and how do I resolve this issue?
After trying several different methods to create a user-enabled, smoothly moving cropping tool I came back to this solution and simply created x and y offsets by programmatically grabbing the constraints' values in the viewDidLoad and then adjusting the origin of imageCropper in viewDidLayoutSubviews and that worked.
However, I have a panGestureRecognizer set up on the ViewController's view. Once I start dragging the photo (imageCropper) to crop it, the imageCropper origin shifts down again. I tried adding the offset logic into the function that handles the recognizer, but it seems to be compounding the change and sending the frame off the screen. (I am resetting the translation with each receipt of a recognizer.)
Code:
override func viewDidLayoutSubviews() { resizedImageFrame = AVMakeRectWithAspectRatioInsideRect(imageToEdit.image!.size, imageToEdit.bounds) let resizedImageCenter = imageToEdit.center let resizedImageX = resizedImageCenter.x - (resizedImageFrame.width / 2) let resizedImageY = resizedImageCenter.y - (resizedImageFrame.height / 2) resizedImageFrame.origin = CGPoint(x: resizedImageX, y: resizedImageY) currentImageFrame = resizedImageFrame imageCropperBorder.frame = resizedImageFrame imageCropper.frame = CGRectOffset(resizedImageFrame, -cropperXOffset, -cropperYOffset) // imageCropper.frame above is set with the offset discussed. If I set it // to simply resizedImageFrame it does NOT appear where imageCropperBorder is } As always, any help would be greatly appreciated.