A view that is moving - iphone

I've no experience in making something move in my app. And I observed this effect in Flickit Pro. The Tap for details view will shake for one or two second and then stop. It looks cute and very user-friendly.
So how can I make effects like this? A gif that is moving? Or some other methods with the help of Cocoa Touch?
Thanks in advance.
Di

A high-level way would be [UIView beginAnimations: context:] (where both parameters can be NULL in this simple case). Then you can just change the properties that want to change of the view that should be animated, add some other "effects" like ease-in/out, etc. pp. When you are done with this, you just call [UIView commitAnimations] and it will animate everything for you.
However, in the case that you need more freedom, look into the CAAnimation class (its inside the QuartzCore framework).
Also: Look into the documentation for both ways (UIView / CAAnimation) and look into session 424 and 425 of the 2010 WWDC.

There are already some answers on stackoverflow on this:
Shake visual effect on iPhone (NOT shaking the device)
UIView shake animation
how to create iphone's wobbling icon effect?
and here is a link that does it on mac:
http://www.cimgf.com/2008/02/27/core-animation-tutorial-window-shake-effect/
I hope these are enough links to help.

Related

UIViewAnimation blocks user interaction

I have a UIView following the users finger as they move it around inside my app. Sometimes, other things on screen are animating with UIViewAnimation blocks, but this freezes the tracking of their finger, so if they continue moving their finger during the animation, it won't follow. How can i stop the animation from blocking up the main thread?
Try using UIViewAnimationOptionAllowUserInteraction with [UIView animateWithDuration:delay:options:animations:completion:]
you can use the NSObject's method: performSelector:onThread:withObject:waitUntilDone:
More details in Apple NSObject Documentation
If there is any other thing on the screen is animated, that would also be done in main thread. And the current finger tracking would also be done in main thread. So definitely there would be some blocking.
To get rid of that, we can optimize our code using blocks and GCD.
Note that if you link for iOS 5 this problem will go away all by itself. Under iOS 5, block-based animation of a view does not turn off user interaction for other views. This is what Apple should have done in the first place.

iPhone Deck Game interface

I wrote the code of a card game .. but now it's time todo the graphic and Interface
What's the best approach to represent a card ??
a) CALayer
b) UIView
c) UIButton
Which one is best to animate and receive user touches???
What do you recommend??
Thanks in advance
GeekGameBoard is older Apple-provided example code for card and board games that implements the UI in CALayers. Another user modified it to run on the iPhone so it might be helpful code for you to look at. The modified project is on bitbucket.
as UIButton is subclass of UIView, there's no so much difference in both of them.
If you need only 'clicks' handling, you can choose button, if you will need to override touchesBegan/touchesMoved you should override UIView instead UIButton.
A UIView subclass would probably be your best choice. You can easily receive touch events from the user, and UIView allows you to do simple animations easily.

Implementing Shake/Slide Function on iPhone SDK

I want to make an app like "10,500 cool facts"
Basically, there is some text on background, and then the user will shake, or slide right/left to navigate between the different facts.
I am new to iPhone programming/SDK, so wondering if anyone could help me get started.
How do I implement a shake / slide function?
First, I urge caution when wanting to use a shake gesture for anything, especially if you are new to iOS programming. It was all the rage when the first apps started coming out, until people started realizing that shaking was being used for just about anything, making it mostly a meaningless feature. It's become, for the lack of a better term, passe.
If you're committed to the shake gesture, you'll want to subclass UIWindow and implement the following methods:
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent*)event;
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent*)event;
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent*)event;
The UIEventSubtype you're looking for is UIEventSubtypeMotionShake.
As for your "slide" function, I assume you're basically talking about paging between screens horizontally. There are a number of ways you can do this, and I think it will boil down to how you're implementing your underlying view controller hierarchy. I haven't done it myself, but if I were, I'd probably just manually detect user touches on my view to determine if someone's swiping left-to-right or right-to-left, and then change views with a matching transition animation.

How do I add animation to an iPhone app?

So I came from a Flash background where I can animate in timeline. I've completed the Beginning iPhone Development book and just realized that I still don't know how to get an animation in. I'm guessing I need to import png sequences?
Can anyone point me to an appropriate place to learn more about this topic? I want to make a game and my game objects need to animate.
Thanks in advance!!
The simplest type of animation, moving things around and fading in and out, can be done with a few static methods of UIVIew. You can affect the center, bounds, transform matrix and alpha level of one or more views.
[UIView beginAnimations:nil context:nil];
[fadingOutView setAlpha:0.0];
[slidingView setCenter:CGPointZero];
[shrinkingView setFrame:CGRectZero];
[fadingInView setAlpha:1.0];
[spinningView setTransform:CGAffineTransformMakeRotation( M_PI )];
[UIView commitAnimations];
Animations start with the current state of the view and interpolate to the state assigned between begin and commit animation. So if fadingInView already had an alpha of 1.0 (the default) there would be no change.
If you are unfamiliar with static methods [UIView method]; means call method on the class not an instance.
Using other UIView static methods you can control several details of the animation. Every UIView has a CALayer that also has a few properties that can be animated, the most interesting of which is the 3D transform property.
If the basic animation is not sufficient for you needs, you can either look into CAAnimation and related classes, or look to a third party animation library.
I think the best place to start learning is in your code, since you are just transitioning from Flash. Look at the very bottom of UIView.h to see the animation methods. Make a few views and move them around.
Take a look at cocos2d for iPhone. It has a good framework for handling sprites, animation (frame based and motion) as well as a lot of other stuff.
http://www.cocos2d-iphone.org
You can of course do all off this on your own with core graphics and core animation, but an API like cocos2d takes care of a lot of the nitty gritty details for you.
The Beginning iPhone Development book does not talk much about animation. You may read more about animation from Apple documentation, and play with some samples from Apple, such as the Touches.

Custom view transition in OpenGL ES

I'm trying to create a custom transition, to serve as a replacement for a default transition you would get here, for example:
[self.navigationController pushViewController:someController animated:YES];
I have prepared an OpenGL-based view that performs an effect on some static texture mapped to a plane (let's say it's a copy of the flip effect in Core Animation). What I don't know how to do is:
grab current view content and make a texture out of it (I remember seeing a function that does just that, but can't find it)
how to do the same for the view that is currently offscreen and is going to replace current view
are there some APIs I can hook to in order to make my transition class as native as possible (make it a kind of Core Animation effect)?
Any thoughts or links are greatly appreciated!
UPDATE
Jeffrey Forbes's answer works great as a solution to capture the content of a view.
What I haven't figured out yet is how to capture the content of the view I want to transition to, which should be invisible until the transition is done.
Also, which method should I use to present the OpenGL view?
For demonstration purposes I used pushViewController. That affects the navbar, though, which I actually want to go one item back, with animation, check this vid for explanation:
http://vimeo.com/4649397.
Another option would be to go with presentViewController, but that shows fullscreen.
Do you think maybe creating another window (or view?) could be useful?
While I cannot completely answer your question without doing some more research of my own, I can help a bit:
-In order to get the view of a UINavigationController, you need to take a screenshot. The easiest way to do this is by grabbing it into a UIImage:
UIGraphicsBeginImageContext(self.view.frame.size);
[[self.view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage* test = UIGraphicsGetImageFromCurrentImageContext();
UIImageView* view = [[UIImageView alloc] initWithImage:test];
UIGraphicsEndImageContext();
I am not sure if you can render a GLContext (not familiar on the phone) into a CGImage, but I would do something like that (and init a UIImage from that). I would prerender every frame of the animation you are trying to do and slap it into an UIImageView using the animation stuff provided within. That is, if your animation is simple enough. Otherwise, it might come down to writing your own animation function :-/
I have just put together a transition class to implement your own transition animation in OpenGL ES.
Feel free to read about it here
There are two example transitions in the project, feel free to add you own to it.
I think the function you might be thinking of is http://www.opengl.org/sdk/docs/man/xhtml/glCopyTexImage2D.xml ... you set the viewport to the texture size and then draw as usual, then do glCopyTexImage2D to copy the scene onto a texture.
or you should look into FrameBuffer Objects. The default OpenGL template in XCode uses these. Just generate the example project to see how those work.
I recently write some transitioning animation betweeen view controllers like you. If you want to get any extra info from the invisible view, you can try delaying the transition like this :
- (void)animationFromModalView:(UIView *)modalView toMasterView:(UIView *)masterView
{
[masterView setNeedsLayout];
[masterView layoutIfNeeded];
[self performSelector:#selector(delayAnimationFromModalViewToMasterView) withObject:nil afterDelay:.1f];
}