iOS - change animation when user taps screen - ios

Lets say that i have an animation - an image is going from left side of the screen to the right. I would like to make it a little bit interactive - when user taps on a screen i want to change direction of image movement. Whats the best approach to implement it?

What I do in some cases is take the main view of the View Controller, in Storyboard, and change the class type of that UIView to UIControl.
In the code that is accessed as MyViewController.view, which you can write:
var viewAsControl = myViewController.view as UIControl
In Swift or some equivalent of that.
The UIControl subclass of UIView is the hierarchical layer (class) that adds the action/target facilities to a view. For example, UIButton is a UIControl, because it generates events (actions), and it is also a UIView so it can be added as a subview.
Then from the Connections Inspector, accessed via the far right Icon of the far right panel (that is, the panel to the right of the storyboard editor window), I'd select the Touch Up Inside event type or some other event and drag it to an #IBAction tagged function I'd add to the View Controller's source code, to receive the tap event. From that tap notification, you can cancel the current animation and add a new one, etc...
Alternatively, you can create an IBOutlet for the view if you've turned it into a UIControl in IB, and use the addTarget() method to assign an action handler for a specific event, e.g. to make it call a function in your code.
Either way the effect will be that any time the view is tapped, it will generate the event for you to respond to

Related

How to make a floating/3D Touch style tableview?

So i'm making an application for tracking livestock. I have made a table view for the animals to go in. I have a navigation controller at the top with a button in side of it.Here is what i have so far.
I would like to have a floating table view come up when the button is pressed similar to the 3D touch menu.
Like these.
My question is how would i go about doing this. Sorry if this is a commonly done thing im pretty new to swift and xcode
If you just wanna to implement,you could search the key words as 'pop over', 'kxmenu' or 'menu' on GitHub/CocoaControls. such like this: https://github.com/zpz1237/NirKxMenu https://github.com/liufengting/FTPopOverMenu
Also, you should make it yourself. When the button is pressed, first you should create a custom window and make it key window. Secondly, add a tableView or a view contains tableView to the custom window. then, use block or delegate to deal with data communication and respond user interaction. After that, design animation you need. At last, remove subviews from custom window and make the original window key window.

Replace views in detail part of the screen after user's action

I am new to Swift and app development. I have a design question. I am trying to make a view that contains a slider, but that as soon as the "touch up inside" action is performed, is replaced by a progress bar + button. If the button is pressed, then we go back to showing only the slider. This view will be not take the whole screen, only part of it.
What would be the best way of doing this? I have already investigated several options:
1. using a navigation controller with a segue triggered by the slider that goes into a new scene with a progress bar & button.
2. creating a custom view with two properties: a slider and a custom view (progress bar & button). The slider can be laid out using interface builder, and the custom view can be loaded from a nib file when needed.
3. creating a custom view with two properties: a slider and a custom view (progress bar & button). The new progress bar and button are created programmatically whenever the action is triggered on the slider.
I have already tried options 1 and 2 to some extent with no success. Since I am a beginner, I am trying to use the IB as much as possible. What is the best option (if any) from the list?
You can do this directly on the Storyboard without needing to create a custom view class, but you'll need a few lines of code in any case. Just drag a Slider into your View, and then drag a button and a progress view directly on top of that. Now select the button, and in the Attributes inspector, tick the box next to "Hidden". Do the same with the progress bar. Then just open the assistant editor and connect references to all 3 of those. You'll also need to create an action for the button (I've called it change), and make sure you leave the type field as AnyObject. Add the following line inside ViewDidLoad:
slider.addTarget(self, action: Selector("change:"), forControlEvents: .TouchUpInside)
This line just makes it so that change gets called anytime the user uses the slider. Obviously change slider to whatever you name your UISlider. You can implement the change function like this:
#IBAction func change(sender: AnyObject) {
slider.hidden = !slider.hidden
button.hidden = !button.hidden
progressBar.hidden = !progressBar.hidden
}
This is a simple implementation that just toggles between true and false for each of the items, but you'll probably want to do it differently depending on what this project does.
Now, if you want to put this functionality in multiple places in your app it might be easiest to create a custom view using the same concept as above, in which case check out this tutorial on how to create an IBDesignable UIView.

How to react to a "touch up inside" event in UIAccessibilityElement subclass?

I have a map with drawn items. The map handles touch events and determines the touched items as if they were buttons.
I made the map a container and implemented the methods to return accessibleElements. For each item I create one instance of a UIAccessibilityElement subclass.
It seems UIAccessibilityAction protocol has no callback for a "tap" or "button pressed" event.
How would I mimic the effect of a UIButton with UIAccessibilityElement then?
Assuming you are running under iOS 5 or iOS 6, consider the following workaround. It is not optimal, but will work until there is a better way:
Create a dummy view that is not, itself, an accessibility element.
On this view, implement your "tap" handling in -touchesEnded:withEvent:.
Set your accessibility element's accessibilityActivationPoint to a value that falls within this dummy view.
Your dummy view will receive touch events when the corresponding accessibility element is activated. Make sure to ignore touch handling in your dummy view if VoiceOver and other assistive technologies are not running.
EDIT: Another less hacky approach is to implement a tap gesture recognizer on the view you're concerned with, convert the coordinate from -touchesEnded:withEvent: to screen coordinates, and manually hit test the point against the frames of your accessibility elements.

How to detect "Drag Enter" on UIScrollView

Imagine a UIButton and a user who taps somewhere OUTSIDE the button and then slides onto it. If I want the button to detect such touches I can register it for any "Touch Drag Enter" events.
My question: is there some way to achieve the same for a UIScrollView?
I.e., I tap somewhere outside the scrollview, drag my finger onto the scrollview and as soon as I enter the scrollviews frame it starts panning? (Because by default it doesn't, I have to start my touch INSIDE the scrollview in order to pan)
If you want to do this, you will have to do a custom implementation using -touchesBegan, -touchesMoved, and -touchesEnded
The documentation for the UIResponder class (which all ViewControllers inherit from) that allows you to do this is here.
Hopefully you can figure out what to do from here. :)
As an extra hint, you will also most likely need to use this function
bool contains = CGRectContainsPoint(CGRect rect, CGPoint point);
Imagine a UIButton and a user who taps somewhere OUTSIDE the button and then slides onto it. If I want the button to detect such touches I can register it for any "Touch Drag Enter" events
Actually, no you can't. A UIControl will not get any of the control events unless the touch started in the control.
And that's for the same reason that you are seeing a similar effect with the scroll view. This is the entire basis of touch delivery on iOS. A touch "belongs" to the view that was initially touched.
After all, the runtime cannot possibly know that you are going to drag into the scroll view...

Using a UIView as a button

I'm trying to use a UIView I've created in Storyboard as a button. I assumed it would be possible to use a UIButton, setting the type to custom. However I was unable to add subviews to a custom UIButton in Storyboard.
As such I've just spent the last hour reinventing the wheel by making my own custom gesture recoginizers to reimplement button functionality.
Surely this isn't the best way of doing it though, so my question - to more experienced iOS developers than myself - is what is the best way to make a custom button?
To be clear it needs to:
Use the UIView I've created as it's hittable area.
Be able to show a
different state depending on whether is currently highlighted or not
(i.e. touch down).
Perform some action when actually tapped.
Thank you for your help.
You can use a UIButton, set the type to custom, and then programmatically add your subviews...
Change your UIView into a UIControl in the storyboard. Then use the method [controlViewName addTarget:self action:#selector(*click handler method*) forControlEvents:UIControlEventTouchDown];. click handler method is a placeholder for the method name of your handler. Use this method except change out the UIControlEventTouchDown for UIControlEventTouchInside and UIControlEventTouchDragExit to call a method when the user finishes their click and drags their finger out of the view respectively. I used this for something I'm working on now and it works great.
In Touch down you will want to: highlight all subviews
In Touch up inside you will want to: unhighlight all subviews and perform segue or do whatever the button is supposed to do
In Touch Drag Exit you will want to: unhighlight all subviews
See second answer by LiCheng in this similiar SO post.
Subclass UIControl instead. You can add subviews to it and it can respond to actions
Why are you implementing your own GestureRecognizer? I recommend using the UIView so you can add subviews in the interface builder and adding a UITapGestureRecognizer. You can even do this graphically since you don't care about IOS4 support.

Resources