Detect touch that originated from superview - ios

I'm creating a subclass of UIView to use in a project, and will handle touches on the main view. I'd like so that when the touch (on the main view) is dragged and contacts the special UIViews, they change their background color. Using UIView's default "touchesMoved" function only detects touches that originate on the specific view. I could set up the main view to check each instance of the custom UIView, but that would be contrary to encapsulation and lead to messy code.
Any ideas?

I'm not sure this is what your after, but if I put the following code in my view controller, whose view has several RDView views in it, the color of those views will change color when I drag from the main view into the RDView.
override func touchesMoved(touches: NSSet, withEvent event: UIEvent) {
var touched = self.view.hitTest((touches.anyObject() as UITouch).locationInView(self.view), withEvent: event)
if touched is RDView {
touched?.backgroundColor = UIColor.redColor()
}
}

Related

Transfer gestures on a UIView to a UITableView in Swift - iOS

I have a layout with a UIView at the top of the page and, right below it, I have a UITableView.
What I am wanting to do is to transfer the gesture interactions on the UIView to the UITableView, so when the user makes a drag up/down on the UIView, the UITableView scrolls vertically.
I tried the following code
tableView.gestureRecognizers?.forEach { uiView.addGestureRecognizer($0) }
but it removed the gestureRecognizers from the UITableView somehow :/
Obs.: the UIView cannot be a Header of the UIScrollView
That's Tricky
What is problem ?
Your top view is not allowed to pass through view behind it...
What would be possible solutions
pass all touches to view behind it (Seems to not possible or very tough practically )
Tell window to ignore touches on top view (Easy one)
Second option is better and easy.
So What you need to do is create subclass of UIView and override
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView?
and return nil if you found same view on hitTest action
Here Tested and working example
class PassThroughME : UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return super.hitTest(point, with: event) == self ? nil : self
}
}
That's it now use PassThroughME either by adding class to your view in storyboard or programmatically whatever way you have added your view
Check image i have black color view with 0.7 alpha on top still i am able to scroll
Hope it is helpful

drag an item out from a scrollview

Am I on the right path trying to drag an item from a UIScrollView and drop it over a second UIScrollView? Neither of the UIScrollViews will be altered in the end, but I'd like to have an image follow the touchMoved position until it's released over second UIScrollView.
I have extended the UIScrollView so I can see where a touch begins inside the UIScrollView.
extension UIScrollView {
override open func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.next?.touchesBegan(touches, with: event)
print("touchesBegan")
}
}
to get the touched object
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
beginningLocation = touch.location(in: self.view)
let position = touch.location(in: itemScrollview)
print("itemScrollview: \(position)")
let whoHit = Int(position.x / 120)
print("whoHit: \(whoHit) \(GameData.items[whoHit].name)")
}
}
I'm trying to use touchesEnded to see if the user dragged up, but touchesEnded doesn't always get called. Works if the touches begin and end over the same UIScrollView, but when dragged off, touchesEnd doesn't get called.
Yo cannot "directly" drag an object (view, image view, label, button, etc) from one view to another (whether it's a UIView or UIScrollView or any other container).
Instead, you need to "move" the object you want to drag from the containing view to the super view (the "main" view), drag it around as a subview of the main view, and then check where it is released.
So, essentially, if you want to drag a UIImageView:
on first touch, move the image view from its containing view to the "main" view
translate the coordinates, so they are now relative to the "main" view
track the touch / drag in main view coordinates, re-positioning the image view as you go
on release, see if the end touch is inside the "target" container view
if so, add the image view as a subview of the target view (translating the position coordinates)
if it's not inside the target frame, either add it back to its original container, or drop it into the main view, or vaporize it, or whatever you desire.

UICollectionView: Measure force on tap of cell

I am working on a tiny project where I want to use the 3D touch screen pressure capabilities (e.g. touch.force).
Right now I can measure force in my ViewController and it is behaving like I want it:
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
if traitCollection.forceTouchCapability == UIForceTouchCapability.available {
// 3D Touch capable
let force = touch.force
print("Force: " + force.description)
}
}
}
I have one UIImageView that presents a picture. I also use an custom UICollectionView as overlay, which contains transparent cells and colored cells. What I am trying to achieve is to measure both force, and be able to remove the color from the cells when they are pressed, but I am failing to do both. I can either measure force, or change the color of a cell in the UICollectionView, but not both.
The solution as I see it is to forward touches from the UICollectionView to the next view in the hierarchy. But another solution could be to measure both force and register taps on cells from within the custom UICollectionView.
My question is, is either of my proposed solutions any good? And if so, how do I achieve them?
Apparently the override func touches... methods are part of UIView, not neccesarily UIViewControllers. This means that implementing the touchesMoved and other methods into my custom UICollectionView is perfectly possible and it solved my problem of handling touches and taps at the same time.

Button in table view cell

I have a custom button and it's added to a static cell. Somehow whenever I tap the button, it doesn't react immediately like when the button is added to a plain normal view.
When tapped, it registers the tap right away but the background color has a slight delay before changing.
To change the color I have the following code in my subclass of UIButton:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesBegan(touches, withEvent: event)
border.fillColor = .redColor() // border is a CAShapeLayer
print("began") // This prints right when the button is clicked
}
This button is added in a subclass of UITableViewCell and the button is reacting, except changing the color has a delay. How can I fix this?
Answer (by Jelly):
The answer of Jelly helped me a lot and made the button react a bit better. Knowing what the problem is also made it easier to research the problem and found out to make the button react just as it would in a normal view I have to disable both delaysContentTouches in the table view and it's subviews:
tableView.delaysContentTouches = false
tableView.subviews.forEach { ($0 as? UIScrollView)?.delaysContentTouches = false }
Set delaysContentTouches = false on your tableView.

TableHeaderView not responding to touches

I have a custom subclass of UIView, designed in IB that contains a few labels and a button.
There is an action for touchUpInside event on the button and the target is the custom view.
I am attaching this custom view to a self.tableView.tableHeaderView for a tableview in my UI.
The strange thing is this custom view is not responding to touches. I can see it nicely with all the labels and the button in side the table view, that means the table view handles and shows it correctly, however it is not responding to touches.
I checked the whole view hierarchy and all the views involved have userInteractionEnabled as YES.
If i drag some other controls into that custom view for example a switch, segmented control..they do not respond as well. It is like these controls in custom view are not registering touches.
It doesn't make any sense. I am out of ideas. Can you help to allow the touch event on the button to arrive to its parent view?
What is a "headerTableView"? Do you mean a UITableViewHeaderFooterView? Have you tried setting userInteractionEnabled on the root UITableViewHeaderFooterView?
This is a hack that will detect a button touch and trigger touchUpInside programmatically:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
{
super.touchesBegan(touches, with: event)
if let touch = touches.first, button.bounds.contains(touch.location(in: button)) {
button.sendActions(for: UIControl.Event.touchUpInside)
}
}
I don’t know what your real problem is because that button should work without this.

Resources