Hello, I am having a problem with my app. I am trying to let the user create an imageView when they click a button and then allow that imageView to be movable. Basically, I want users to be able to move characters around like for blocking a camera shot or something (for directors). However, when the imageView is created, and I try to move it, for some reason it only recognizes my touch when I touch the screen about an inch to the right and an inch below the image. Normally, when you hold down on a image it follows your finger, but in my app that only works if you hold your finger on a blank space a few inches away from the image. So basically you end up moving the image, but it’s about an inch from your finger. I’m not sure if I’m explaining this correctly, but here is my code (part of it). I have touchesBegan and touchesEnded as well but they have the same exact code as touchesMoved. Any help would be greatly appreciated! Also, is there a simple way I can confine these movable objects in my app to a certain larger imageView? In other words, I don’t want the user to move the character or object to the edge of the screen but just to the edge of an imageView that covers a large part of the screen. Thanks in advance!
override func viewDidAppear(_ animated: Bool) {
observer = NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "createThatCharacter"), object: nil, queue: OperationQueue.main) { (notification) in
// Create an imageView to house a character object
self.imageView = UIImageView(frame: CGRect(x: 150, y: 150, width: 50, height: 50))
// Put the character image into the imageView
self.imageView.image = UIImage(named: "character")
self.imageView.contentMode = .scaleToFill
// Append the imageView to an array of character imageViews
characterArray.append(self.imageView)
self.view.addSubview(self.imageView)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in (touches) {
let location = touch.location(in: sandboxImageView)
for image in characterArray {
if image.frame.contains(location) {
image.center = location
}
}
for image in objectArray {
if image.frame.contains(location) {
image.center = location
}
}
}
}