Flipping a view that is contained on a view setup - objective-c

I currently have a xib that is a standard view that has another view contained on top. I want the user to be able to click on this subview, and then have this subview flip over when pressed. I currently have code that flips the view around, but since I'm not explicitly assigning a new view, it just flips over and displays the same view. Here is my code when the button is pressed:
- (IBAction)flipCard:(id)sender
{
NSLog(#"Subview button was clicked!");
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:self.frontOfView
cache:YES];
[UIView commitAnimations];
}
The biggest question is how to set this up. Do I need to create an entire view programmatically and then just call [[self view] addSubview: backOfView]*? Or is there a way to do this with interface builder?
*Note I haven't actually create a "backOfView" object, I'm not sure how I should create one at this point.

Try this one?
[UIView transitionFromView:self.frontOfView
toView:self.backOfView
duration:0.4f
options:UIViewAnimationOptionTransitionFlipFromLeft
completion:nil];

Related

Objective c, reposition a view above the keyboard in one single animation

I have a custom view at the bottom of my screen, i want to reposition it above the keyboard when it opens up.
I know i can register for the 'KeyboardWasShown' notification and then reposition the view or as the apple documentation suggests to use a scrollview with scrollRectToVisible, but the problem i have with both options is that it is not done as part of the keyboard animation.
I can see the keyboard appear and only a second later the view is repositioned or scrolled in to view.
Another option i tried was to set this view as InputAccessory for the textView im editing, This works very well but my view will not be visible once the keyboard is closed, and i need it to be available all the time.
I suppose i can create two instances of this view and have one as inputAccessory and the other just sitting in the bottom, but i really dont like this solution and these views have a state that will have to be synced between the two instances
Can anyone suggest an alternative solution ?
You better use UIKeyboardWillChangeFrameNotification notification.
Add observer first.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
The selector will be...
- (void)keyboardFrameChange:(NSNotification *)notification {
CGRect begin = [[notification.userInfo valueForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
CGRect end = [[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
CGFloat keyboardSize= begin.size.height;
BOOL isShowing = begin.origin.y > end.origin.y;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:[notification.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]];
[UIView setAnimationCurve:[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]];
[UIView setAnimationBeginsFromCurrentState:YES];
// do view animation here.
[UIView commitAnimations];
}
The animation of your view and the keyboard will be sync.

Strange results using CGAffineTransformIdentity

I have several UI elements in a nib that are positioned using autolayout and constraints. When the view loads i move the elements off screen using
[self.button setTransform:CGAffineTransformMakeTranslation(-self.button.frame.origin.x * 2 + self.button.frame.size.width, 0)];
then when the view appears i use the following to ease the button into the correct position on the page. However it seems to move too far across.
[UIView beginAnimations:nil context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView setAnimationDuration:2];
[self.button setTransform:CGAffineTransformIdentity];
[UIView commitAnimations];
Am i doing something wrong? Is there a better way to move the button off the screen with CGAffineTransform?
Any help would be great
The issue was caused by autolayout and the best way to animate UI elements when using autolayout is to change the constraints

How can I touch and drag an animated image? here's my animation code:

- (void)viewDidLoad
{
[super viewDidLoad];
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint abc =chem1.center;
abc.y= 480;
chem1.center=abc;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint def =chem2.center;
def.y= 480 ;
chem2.center=def;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint hij =nat1.center;
hij.y= 480;
nat1.center=hij;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:4];
[UIView setAnimationRepeatCount:50];
[UIView setAnimationRepeatAutoreverses:NO];
CGPoint klm =nat2.center;
klm.y= 480;
nat2.center=klm;
}
I'd really appreciate the help
There are two parts to your question: how to tap an animated image and how to drag an image.
How to tap an animated image.
Background
When you are animating a view (or more accurately its layer) the property that you change (in your case the position (by setting center)) is changed to its final value right away. If you only animate (like with a CABasicAnimation, then the properly might not change at all.
This means that if you try to tap the view (or layer) it will be at the position that you moved it to (its final position (despite any duration or repeat count of the animation).
Hit test a moving layer
To be able to hit test something that is moving you need to hit test against the presentationLayer of the layer of the view that you are trying to tap.
CGPoint touchPoint = [gestureRecognizer locationInView:[self view]];
if ([[[[self movingView] layer] presentationLayer] hitTest:touchPoint]) {
// Yeah, you hit the moving (animating) layer!
}
Now that the user has tapped a moving view (image in your case) you can begin to handle the drag.
How to drag an image
When you are dragging an image you move the layer to wherever the users finger is dragging the image.
Remove to animation that moves the view
First you should cancel the ongoing animation for that view (having a name for the animation enables you to cancel only one animation.
[[[self movingView] layer] removeAllAnimations];
Or if you have set a name to the movement animation.
[[[self movingView] layer] removeAnimationForKey:#"nameOfMovingAnimation"];
Move the view to the location of the users finger
Next you should (continuously) update the position of the view as the user moves its finger. You should not animate this since it will increase the time between the movement of the finger and the view and will make it seem like your application is lagging. Animating while dragging does not make it look smooth.
Note: since your question doesn't say what you want to do once the user are dragging the images or what happens when they drop them. I can't go much further in the explanation.
Need more help?
Search the web (or StackOverflow) for something like: "iOS drag view" and you will probably find more information about how to drag your imageViews.
Otherwise ask another question on StackOverflow once you have gotten this far (i.e. you can tap on the moving images and they stop moving).

Change backside color of uiview when using Curlup animation

I have an uiview which i add a transition that removes it from an uiwindow using curlup animation. When the animation occurs the backside of the view is white...i would like to change the color of it or even put some of my own texture. Any help appreciated.
[UIView beginAnimations: #"Curl up" context:nil];
// wait for time before begin
[UIView setAnimationDelay:wait];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:viewToCurlUp cache:YES];
// druation of animation
[UIView setAnimationDuration:duration];
[UIView commitAnimations];
As far as I know, it is not possible to create a texture (ie: your own image) on a UIView. The color of 'back of the curl' should correspond to the background color you have set for your UIView. You can do this in the inspector in Interface Builder, or programmatically like so:
[view setBackgroundColor:[UIColor greenColor]];
where view is the UIView of interest. Use the appropriate color, of course.
Hope that helps.
Apple engineers responded that this is not possible in my case. I have asked them a few days ago.

Animating Viewcontroller

I am working on a project were i need to hide the view and when I click on a button the view should appear, its some thing like animating the view in one view controller?
Thanks.
when clicking a button you will call a method, so in that method write the below code and you hide the view of you added in view. for example if you add a button means remove that.
[button.view removeFromSuperView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.5f];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:[self view] cache:YES];
[UIView commitAnimations];