How to interact with UIView, which is subview UIScrollView?

There is a UIScrollView there are a few UIView, the movement you want to track using touchesMoved(), but UIScrollView intercepts the touch and does not interact with the UIView.
To move and Scroll content and interact with UIView.
How can this be solved?
June 8th 19 at 17:31
1 answer
June 8th 19 at 17:33
Solution
Try:
var panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(move))
panRecognizer.minimumNumberOfTouches = 1
panRecognizer.maximumNumberOfTouches = 1
panRecognizer.delegate = self
yourView.addGestureRecognizer(panRecognizer)

@objc func move(_ sender: Any) {
 if (sender as? UIPanGestureRecognizer)?.state == .changed {
 // This will return you location in view
 let currentPoint: CGPoint = sender.location(in view)
 // This will return you location in Scrollview
 let scrollPoint: CGPoint = sender.location(in: sender.view.superview)
}
}
Could not cast value of type "MyViewController' (0x10eb38980) to 'UIGestureRecognizerDelegate' (0x112aee998).
Could not cast value of type 'MyViewController' (0x10eb38980) to 'UIGestureRecognizerDelegate' (0x112aee998). - Chad93 commented on June 8th 19 at 17:36
all Your code show. - alycia_Pfannerstill commented on June 8th 19 at 17:39
import UIKit

class MyViewController: your uiviewcontroller {

 @IBOutlet weak var scrollView: UIScrollView!

 var slider = UIView()



 override func viewDidLoad() {
super.viewDidLoad()

 scrollView.contentSize = CGSize(width: scrollView.frame.size.width*2, height: scrollView.frame.size.height)
 scrollView.backgroundColor = UIColor.lightGray

 var panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(move))
 panRecognizer.minimumNumberOfTouches = 1
 panRecognizer.maximumNumberOfTouches = 1
 panRecognizer.delegate = self as! UIGestureRecognizerDelegate 

 slider.frame = CGRect(x: 30 + 30*i, y: 30 + 60*i , width: 50, height: 50)
 slider.backgroundColor = UIColor.yellow
scrollView.addSubview(slider)

slider.addGestureRecognizer(panRecognizer)

}



 @objc func move(_ sender: Any) {
 if (sender as? UIPanGestureRecognizer)?.state == .changed {
 // This will return you location in view
 let currentPoint: CGPoint = (sender as of anyobject).location(in view)
 // This will return you location in Scrollview
 let scrollPoint: CGPoint = (sender as of anyobject).location(in: sender.view.superview)
}
}


 override func touchesMoved(_ touches: Set<uitouch>, with event: UIEvent?) {
 super.touchesMoved(touches, with: the event)
 let touch: UITouch = touches.first as! UITouch

 var newCoord = touch.location(in: self.view)
 slider.frame = CGRect(origin: CGPoint(x: newCoord.x , y: newCoord.y size: the slider.frame.size)



 }//end of func

}//end of class</uitouch>
- Chad93 commented on June 8th 19 at 17:42
, self as! UIGestureRecognizerDelegate why as! UIGestureRecognizerDelegateif you don't implement this delegate? - alycia_Pfannerstill commented on June 8th 19 at 17:45
Xcode says error and makes to add - Chad93 commented on June 8th 19 at 17:48
get the row with the delegate at all. - alycia_Pfannerstill commented on June 8th 19 at 17:51
a delegate you had in your code
in General, the problem in the line:
let scrollPoint: CGPoint = (sender as of anyobject).location(in: sender.view.superview)

namely
Cannot call value of non-function type 'CGFloat'


and how do I use this code to interact with UIView? I also need it to move(for scrolling but not scroll) - Chad93 commented on June 8th 19 at 17:54
i.e. you should move and scrollview and something on UIView? - pasquale commented on June 8th 19 at 17:57
you need to move the content of the ScrollView and UIView for this content

what is written above a few alterations and will of the armed forces] to work - Chad93 commented on June 8th 19 at 18:00

Find more questions by tags SwiftiOS