UIButton on iPhone -- making the button shake or wiggle - iphone

I have been working on this for a few days with no luck... I am using Interface Builder to build the views. I have a UIButton with an image as the background. When I shake the iPhone, I want the button to wiggle. Any ideas?

Wobbling effect code here: how to create iphone's wobbling icon effect?.
There are examples of shake detection in Apple sample code so I won't repeat it here. You'll want to set a threshold for how hard you want it shaken (and maybe in what direction, if you care). Once it reaches that point then just trigger the routine that kicks off the animation effect. You may want to turn off shake detection once it's started and then turn it back on in the animation completion handler.

I presume you'd have to change the CGRect defining the frame in which the button is drawn.

Related

Pausing and restarting animations

I'm working on a test that uses swift UIKit dynamics to animate a ball across the screen. I want to add a button in the top left of the screen, that when pressed, pauses the screen just how it is, then when pressed again, resumes the animation. If anyone has any suggestions on how to go about doing this that would be much appreciated. Thank you
The simplest thing to do is to set the speed of the layer of a superview that contains all views being animated to 0:
self.view.layer.speed = 0
Then set it back to 1.0 when you want the animation to continue.
(It isn't obvious that layers have a speed property. It is there because they conform to the CAMediaTiming protocol.)

How to gently drag panel from offscreen like JetBlue iPhone app

I'm looking for a way to slide a partially concealed tool panel from off screen like the JetBlue iPhone app does. I know how to do this with regular swipe gesture recognizers, but that app has a certain threshold, after which the panel "snaps" to the on-screen position, otherwise it hides back offscreen.
How can I implement this kind of swipe-to show, but with a threshold kind of gesture recognizer? Are there any open source projects that do this kind of UI manipulation?
I have a similar interface element in an app I've written. My interface element is a UIViewController for the entire tool panel. I put a UIButton over the gripper illustration, change the button's type to custom so it visually disappears. I connect Touch Drag Inside and Touch Drag Outside to an IBAction that adjusts the position of the panel according to where the drag moves. I connect Touch Up Inside and Touch Up Outside to an IBAction that finalizes the positioning of the view. If the touch up event happens too close to the bottom, I just close the panel. If it happens anywhere higher than that threshold I open the panel. I use UIView's animation methods to smooth out these transitions. The over extension element seen in the JetBlue app can be accomplished in the drag event handler. As the panel gets closer and closer to the limit, open the view a smaller and smaller amount with each move of the touch higher. Then in the finalization, just animate the panel back down into it's preferred ending position.
The JetBlue app differs from my app in that I have a small gripper area, but JetBlue's app allows you to swipe the whole panel to adjust position. To match that functionality, I would adjust my implementation to totally cover the panel with buttons. Each would respond to my drag events with the addition that dragging would set a flag. Then my touch up event handler would check if the flag was set. If so, finalize the dragging of the panel. Otherwise, perform the appropriate action associated with each button.
If you can already implement the basics with gestures, then you're almost there!
To be honest, while I've done exactly this in my application, I use the old fashioned touchesBegan, touchesMoved, etc.
In terms of gestures, you'll have to use UIPanGestureRecognizer so you can have full control of the drag. UISwipeGestureRecognizer only recognizes swipes.
Anyway, after a certain point, you simply translate the panel only a fraction of the distance the person dragged.
CGRect newPanelFrame = panel.frame;
if (newPanelFrame.origin.y + dragOffset > 275) {
newPanelFrame.origin.y += dragOffset / 2.0;
}
panel.frame = newPanelFrame;
In touchesEnded:withEvent: or if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
CGRect newPanelFrame = panel.frame;
if (newPanelFrame.origin.y > 275) {
newPanelFrame.origin.y = 275;
}
panel.frame = newPanelFrame;
The reason why I've never bothered with the UIPanGestureRecognizer is because I could never figure out how to get the non-cumulative translation (translationForView: is cumulative), which is necessary if you want to essentially slow down the drag after the threshold.
I was just doing some research into achieving a similar effect, stumbled across this post, and as a result decided to give JetBlue a try.
After playing with the sliding panel for a bit, I'm actually thinking the JetBlue guys actually went about this in a slightly more different manner.
The motions of the slider in the JetBlue app seem to handle much nicer than what you could do with a collection of UIGestureRecognizers; it's capable of animating at different velocities depending how quickly the user swipes it, and the rubber-banding compensates for this properly too.
So on that note, I actually think that they're not using UIGestureRecognizers, but that panel is actually just a standard UIScrollView aligned to the bottom of the app window, and with a bit of extra logic (probably applied via the delegate) that just makes sure it isn't possible for the scroll view to stop animating between the 'open' and 'shut' state.

How can I get notified with continous touch down events from my button instead only one?

I must rotate an image but I can't use the gestures or the slider, so I have thinked about using 2 buttons, one for clockwise rotation and the other for couterclockwise rotation of my UIImageView.
But I'm getting notified only one time for touch down event so my image rotate only one time.
There is a way to get continous touch down event when I press my button?
Is there isn't such opportunity, how can I implement this continous rotation with the two buttons?
Sorry if I've said something wrong I'm new to iOS development.
There's a couple of ways you could do this - UIButtons have touchDown and touchUp events, so you could start a process on touchDown and end it on touchUpInside. This discussion might be useful.
Alternatively, rather than using UIbuttons you might want to try detecting a touchesBegan in a particular area (say on a UIImageView containing a button-lie image), then running the rotation animation until the touchesEnded event occurs?

How does iPhone draw controller?

I'm a newbie in iPhone development and now encounter a problem:
I want to add a small hotspot on a large image and simply came up with the idea that I can add a button in that area setting alpha 0 to hide the button. Unfortunately, the phone does not response click events any more. Instead, if I increase the alpha, I can receive click events without any problems. So I wonder in which order does iPhone draw controller on the screen. Is it because my background image is drawn after the button if I set alpha of button too low, so the click event is intercepted by the background image?
Any hints will be highly appreciated. Thank you all in advance!
Best Regards.
Set the button's style to Custom; the button will not be drawn, but should remain fully active.
The problem is not the order in which the views are drawn, it's rather that if a view's alpha is 0 it does not receive touch events. it's like the view is not there at all.
but you don't have to set the alpha to 0 on the UIButton, you can simply make it a Custom button instead of a Rounded Rect button. it'll be invisible and still work.

How can I detect a touch on an animated subview ? Or click an animated UIButton?

I have a view that is moving. The view is sometimes on and sometimes off screen - it is twice the width of the screen.
In this view, there are buttons that I need to be able to click.
I've tried doing this in many ways already - using pure buttons
using touches began on UIView
I'm doing the animation using a CGPath in core animation
Any help would be most appreciated
Thanks
Whether the view is moving or not should irrelevent to the touch detection. If you have a UIButton object and are handling the UIEventControlTouchUpInside (forgot the exact name) event , it should be called when it sees a touch. Is that not working for you?