Hello all you awesome peeps! I have been having an odd issue where if UIView is animated, in my case rotated, during the animation the subviews touch locations are inaccurate.
For example, if I keep clicking button1, sometimes button2 will be pressed or nothing will be pressed. I've searched on STO and other blogs, trying their methods, but nothing seems to work. My goal is to have buttons clickable when the view is rotating.
I thought it was a UIButton bug or issue, but even with imageViews, and touch delegates or added Gestures, I get the same thing. I feel like something simple, like the presentation.layer or something that needs to be added to the UIView animate function, but I'm kinda at a loss. Hope you can shed some insight! Thank you all in advance.
func updateRotateView ()
{
UIView.animateWithDuration(2,
delay: 0.0,
options: [UIViewAnimationOptions.CurveLinear, UIViewAnimationOptions.AllowUserInteraction],
animations: {
self.rotationView.transform = CGAffineTransformRotate(self.rotationView.transform, 3.1415926)
//Test A
//self.button1.frame = self.rotationView.convertRect(self.button1.frame, fromView:self.rotationView)
//self.button1.layer.frame = self.rotationView.layer.convertRect(self.button1.layer.frame, fromLayer: self.rotationView.layer)
}
completion: {finished in })
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
{
//Test B
let firstTouch = touches.first
let firstHitPoint = firstTouch.locationInView(self.rotationView)
if (self.button1.frame.contains(firstHitPoint)) {
print("Detected firstHit on Button1")
}
if (self.button1.layer.frame.contains(firstHitPoint)) {
print("Detected firstHit on Button1.layer")
}
if ((self.button1.layer.presentationLayer()?.hitTest(firstHitPoint!)) != nil) {
print("Detected button1.layer hit test")
}
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
//Test C
let lastTouch = touches.first!
let lastHitPoint = lastTouch.locationInView(self.rotationView)
if (self.button1.frame.contains(lastHitPoint)) {
print("Detected lastHit on Button1")
}
if (self.button1.layer.frame.contains(lastHitPoint)) {
print("Detected lastHit on Button1.layer")
}
if ((self.button1.layer.presentationLayer()?.hitTest(lastHitPoint!)) != nil) {
print("Detected button1.layer hit test")
}
}
extension UIButton {
//Test D
public override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView?
{
print("hitTest called")
}
public override func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool
{
print("pointInside called")
}
}
Test A -> rotationView.presentationLayer value changes, but the buttons do not, and setting it to an updated frame, is not working as the button1 frame or layer.frame size values do not change.
Test B/C -> UIButton does not respond to touch delegates, but even with imageViews does not hit accurately. Also did try Gestures with same result.
Test D -> Hitpoint does get called, but most likely on the wrong button as well. Point inside only gets called few times and if I am able to press the correct button, but again very inaccurate.
Because you are animating a view. You should know that there is a presentation layer and a model layer when animating.
Once self.rotationView.transform = CGAffineTransformRotate(self.rotationView.transform, 3.1415926)
is called in the animation block, the transform of rotationView has already been set, and the animation you see is just the presentation layer(It is an id type but it should be CALayer).
Related
I built a SplitView class that looks like this picture below:
As you can see the SplitView always have two subviews, therefore it has two properties that are leftView and rightView.
The job of the SplitView is to manage its subview size proportion.
If I do a swipe gesture to the left, it will move the separator location and it changes the size of each subviews, so it will look like this:
It works perfectly until I use UITableView as the leftView and rightView.
This is because the UITableView inside it is the one which will process the touch event, not the superview (which is SplitView)
And because the code to intercept and respond to the touch is in SplitView, it doesn't do anything if the UITableView inside it is the one that receives the touch event.
To do this I implement hitTest to make the SplitView the responder, not the UITableView inside it.
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return self
}
Now I can get the touch event in the SplitView even though the user swipe on the UITableView.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first {
self.startInfo = StartInfo(timestamp: touch.timestamp,
touchLocation: touch.location(in: self),
separatorLocation: separatorView.frame.origin)
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
if let touch = touches.first, let startInfo = startInfo {
let location = touch.location(in: self)
if moveHorizontally == nil {
let deltaX = abs(location.x - startInfo.touchLocation.x)
let deltaY = abs(location.y - startInfo.touchLocation.y)
if deltaX > 4.0 || deltaY > 4.0 {
moveHorizontally = deltaX > deltaY
}
}
if let moveHorizontally = moveHorizontally {
if moveHorizontally {
print("the user intends to adjust separator position")
adjustSeparatorViewPosition(usingTouchLocation: location)
} else {
print("the user intends to scroll the table view")
if rightView.frame.contains(location) {
rightView.touchesMoved(touches, with: event) // doesn't work
} else {
leftView.touchesMoved(touches, with: event) // doesn't work
}
}
}
}
}
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
startInfo = nil
moveHorizontally = nil
leftView.touchesEnded(touches, with: event)
guard let touch = touches.first else { return }
if touch.location(in: self).x < self.bounds.width / 2 {
UIView.animate(withDuration: 0.3) {
self.setSeparatorLocationX(to: self.separatorMinX)
}
} else {
UIView.animate(withDuration: 0.3) {
self.setSeparatorLocationX(to: self.separatorMaxX)
}
}
}
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
startInfo = nil
moveHorizontally = nil
leftView.touchesCancelled(touches, with: event)
}
As you can see in this code, I added a delay, just like UIScrollView does when swiping to get the user intention, whether the user want to scroll horizontally or vertically.
- If the swipe direction is horizontal, I want to adjust the separator location
- If the swipe direction is vertical, I want to forward the event to the UITableView inside it (for example the leftView)
But, forwarding the touch event using rightView.touchesMoved(touches, with: event) doesn't work.
How to forward the touch event from the SplitView to the UITableView inside it?
The isHighlighted property on UIButtonis mostly analogous to whether the button is pressed or not.
The Apple docs are vague about it:
Controls automatically set and clear this state in response to
appropriate touch events.
https://developer.apple.com/documentation/uikit/uicontrol/1618231-ishighlighted
Which method is the system calling to set this?
It appears to be touchesBegan, because you can override this method to prevent it.
But, surprisingly, if you manually call touchesBegan it doesn't get set (for example, from a superview-- see code below).
So it seems it's not simple as touchesBegan. I've tried overriding every method I could...pressesBegan, pressesChanged, pressesCancelled, pressesEnded, touchesCancelled, beginTracking, continueTracking, hitTest, etc.
Only touchesBegan had any effect.
Here's how I tried calling touchesBegan from a superview:
class MyView: UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let point = touches.first?.location(in: self) else { return }
for view in self.subviews {
if let button = view as? UIButton {
if button.frame.contains(point) {
button.touchesBegan(touches, with: event)
print("\(button.isHighlighted)") //prints false
}
}
}
}
}
I have subclassed a UITableView in my app so that I can intercept touch events. I am using this to allow me to provide 3D Touch gestures on the entire view (including on top of the table view).
This works great, however the problem is that using 3D Touch on one of the cells and then releasing your finger activates the cell tap.
I need to only activate the cell tap if there is no force exerted. I should explain that I am fading an image in gradually over the entire screen as you apply pressure.
Here is my subclass:
protocol PassTouchesTableViewDelegate {
func touchMoved(touches: Set<UITouch>)
func touchEnded(touches: Set<UITouch>)
}
class PassTouchesTableView: UITableView {
var delegatePass: PassTouchesTableViewDelegate?
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesMoved(touches, withEvent: event)
self.delegatePass?.touchMoved(touches)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
super.touchesEnded(touches, withEvent: event)
self.delegatePass?.touchEnded(touches)
}
}
And here are the methods I'm calling from my view controller when the touches end and move:
internal func touchMoved(touches: Set<UITouch>) {
let touch = touches.first
self.pressureImageView.alpha = (touch!.force / 2) - 1.0
}
internal func touchEnded(touches: Set<UITouch>) {
UIView.animateWithDuration(0.2, animations: {
self.pressureImageView.alpha = 0.0
})
}
You could create a boolean named isForceTouch which is set to false in touchesBegan, and then once force touch is detected, set it to true. Then in didSelectRowAtIndexPath just return false if isForceTouch is true. It may need tweaking but that should work.
I have a UITableView with UITableViewCell like this:
I added a UITapGestureRecognizer (let's call the target targetCell) on the tableView to manage when the user tap on a cell. On the Button, let's call the target targetButton.
On top of the cell, I added a UIScrollView as an overlay.
What I would like to do it that when there is a pan/swipe anywhere on the cell/scrollView, then the pan is detected.
But if there is a tap on the button then targerButton is triggered, if it's outside targetCell is triggered.
So far, only targetCell is triggered, inside or outside of the button area. If I remove the UIScrollView, it works fine.
So at first I thought about overriding func pointInside(point: CGPoint, withEvent event: UIEvent?) -> Bool. The problem is that at this stage, it doesn't make the difference between a tap and a pan so it doesn't solve my problem.
Then I thought about overriding func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) like the following:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let location = touches.first?.locationInView(tableView) {
var buttonFrame = CGRectMake(frame.width - 50, 0, 50, bounds.height)
buttonFrame.origin.y = (convertRect(buttonFrame, toView: tableView).minY)
let toNextResponder = buttonFrame.contains(location)
if toNextResponder {
self.nextResponder()?.touchesBegan(touches, withEvent: event)
} else {
super.touchesBegan(touches, withEvent: event)
}
} else {
super.touchesBegan(touches, withEvent: event)
}
}
It didn't work. Then I realised that self.nextResponder()? was the UITableViewCellContentView so I thought maybe, call touchesBegan(touches, withEvent: event)directly on the button. So I changed touchesBegan to this:
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if let location = touches.first?.locationInView(tableView) {
var buttonFrame = CGRectMake(frame.width - 50, 0, 50, bounds.height)
buttonFrame.origin.y = (convertRect(buttonFrame, toView: tableView).minY)
let toNextResponder = buttonFrame.contains(location)
if toNextResponder {
if let cellContentView = self.nextResponder() as? UIView {
let button = findButtonInView(cellContentView)
button?.touchesBegan(touches, withEvent: event)
}
} else {
super.touchesBegan(touches, withEvent: event)
}
} else {
super.touchesBegan(touches, withEvent: event)
}
}
private func findButtonInView(view: UIView) -> UIButton? {
if view is UIButton {
return view as? UIButton
} else {
var button: UIButton?
for subview in view.subviews {
button = findButtonInView(subview)
if let _ = button {
return button
}
}
return button
}
}
And... it doesn't work, targetCell only gets triggered whether you tap on the button or not. I noticed something though it that when I call chartButton?.touchInside I get false.
What did I do wrong? Or what should I have done?
Well I found a solution but I don't really like it as it feels more like a hack than a proper one.
I basically execute the button's target when a tap gesture is recognized within the button's area:
override func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer is UITapGestureRecognizer {
let location = (gestureRecognizer as! UITapGestureRecognizer).locationInView(tableView)
var buttonFrame = CGRectMake(frame.width - 50, 0, 50, bounds.height)
buttonFrame.origin.y = (convertRect(buttonFrame, toView: tableView).minY)
if buttonFrame.contains(location) {
button.sendActionsForControlEvents(.TouchUpInside)
return false
}
}
return super.gestureRecognizerShouldBegin(gestureRecognizer)
}
If anyone has a better way or think my way is wrong please let me know.
I'm making a calculator app that has several UIButtons for input of digits etc. I want the user to be able to touch down on one button and, if this was not the intended button, move the finger to another button and touch up inside that one. The button that the user has his/her finger on should change background color to indicate to the user what is happening, much like Apples built in calculator app.
I've tried to do this by using touch drag inside/outside and touch drag enter/exit on the buttons, but it only works for the button where the touch originated. Meaning I can touch down on one button, drag outside, back inside and touch up inside, but I can't touch down, drag outside and touch up inside another button.
Also the area that is recognized as being inside or outside the button is larger than the bounds of the button.
Here's an example of the code I've tried for one of the buttons:
#IBAction func didTouchDownThreeButton(sender: AnyObject) {
threeButton.backgroundColor = blueColor
}
#IBAction func didTouchUpInsideThreeButton(sender: AnyObject) {
inputTextView.text = inputTextView.text + "3"
threeButton.backgroundColor = lightGrayColor
}
#IBAction func didTouchDragExitThreeButton(sender: AnyObject) {
threeButton.backgroundColor = lightGrayColor
}
#IBAction func didTouchDragEnterThreeButton(sender: AnyObject) {
threeButton.backgroundColor = blueColor
}
Any help would be much appreciated!
I managed to create a suitable solution by overriding the functions below and keeping track of which button was touched last and second to last. The last button touched gets highlighted and the second to last gets unhighlighted. Here's my code for a two button test in case anyone should find it useful:
#IBOutlet weak var bottomButton: UIButton!
#IBOutlet weak var topButton: UIButton!
var lastTouchedButton: UIButton? = nil
var secondToLastTouchedButton: UIButton? = nil
override func touchesBegan(touches: Set<UITouch>?, withEvent event: UIEvent?) {
let touch = touches?.first
let location : CGPoint = (touch?.locationInView(self.view))!
if topButton.pointInside(self.view.convertPoint(location, toView: topButton.viewForLastBaselineLayout), withEvent: nil) {
topButton?.backgroundColor = UIColor.redColor()
}
else if bottomButton.pointInside(self.view.convertPoint(location, toView: bottomButton.viewForLastBaselineLayout), withEvent: nil) {
bottomButton?.backgroundColor = UIColor.redColor()
}
super.touchesBegan(touches!, withEvent:event)
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let location : CGPoint = (touch?.locationInView(self.view))!
if topButton.pointInside(self.view.convertPoint(location, toView: topButton.viewForLastBaselineLayout), withEvent: nil) {
secondToLastTouchedButton = lastTouchedButton
lastTouchedButton = topButton
lastTouchedButton?.backgroundColor = UIColor.redColor()
}
else if bottomButton.pointInside(self.view.convertPoint(location, toView: bottomButton.viewForLastBaselineLayout), withEvent: nil) {
secondToLastTouchedButton = lastTouchedButton
lastTouchedButton = bottomButton
lastTouchedButton?.backgroundColor = UIColor.redColor()
}
else {
lastTouchedButton?.backgroundColor = UIColor.whiteColor()
}
if secondToLastTouchedButton != lastTouchedButton {
secondToLastTouchedButton?.backgroundColor = UIColor.whiteColor()
}
super.touchesMoved(touches, withEvent: event)
}
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch = touches.first
let location : CGPoint = (touch?.locationInView(self.view))!
if topButton.pointInside(self.view.convertPoint(location, toView: topButton.viewForLastBaselineLayout), withEvent: nil) {
topButton?.backgroundColor = UIColor.whiteColor()
}
else if bottomButton.pointInside(self.view.convertPoint(location, toView: bottomButton.viewForLastBaselineLayout), withEvent: nil) {
bottomButton?.backgroundColor = UIColor.whiteColor()
}
super.touchesEnded(touches, withEvent: event)
}
override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {
lastTouchedButton?.backgroundColor = UIColor.whiteColor()
super.touchesCancelled(touches, withEvent: event)
}
I've tried to do this by using touch drag inside/outside and touch drag enter/exit on the buttons, but it only works for the button where the touch originated
Absolutely right. The touch "belongs" to a view, and for as long as you keep dragging, it will still only belong to that view. Thus, the only way something like what you describe can be possible is for the touch detection to take place in the common superview of these button views.