addsubview after another subview - iphone

I know its mad but here it goes:
i load a splashView to my app: on viewDidLoad:
[self.view addSubview:splashView];
UIViewAnimationOptions options = UIViewAnimationOptionCurveLinear;
[UIView animateWithDuration:0.5 delay:3.0 options:options animations:^{
splashView.alpha = 0.0f;
splashView.frame = CGRectMake(512,384,-10,-10);
}
completion:^(BOOL finished){
[splashView removeFromSuperview];
}];
Nothing special to the code here.
however, when i try to load the same view as an action through a button
NOTHING happens:
-(IBAction)showSplash:(id)sender {
[self.view addSubview:splashView];
UIViewAnimationOptions options = UIViewAnimationOptionCurveLinear;
[UIView animateWithDuration:0.5 delay:3.0 options:options animations:^{
splashView.alpha = 0.0f;
splashView.frame = CGRectMake(512,384,-10,-10);
}
completion:^(BOOL finished){
[splashView removeFromSuperview];
}];
}
why is this happening?
By the way if i DO NOT load the splashView on the viewDidLoad and then use the button (action) it works ok!
is this normal behavior?

Reset the alpha value before fading the splash view out.
splashView.alpha = 1.0f;

Related

How to repeat animation from current state not from startingstate?

I want to move my UIView from left to right in a repeatedly till the view not reached at the boundary of the right side. the below code running repeatedly but move only 5 points and repeat from starting point i want to repeat from last current state and the +5 to move further.
CGPoint pos = mover.center;
pos.x=25;
int count = 25;
[UIView beginAnimations:nil context:NULL];
pos.x=count;
[UIView setAnimationDuration:0];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationRepeatCount:INFINITY];
//[UIView setAnimationRepeatAutoreverses:YES];
// CGPoint pos = mover.center;
//pos.x +=5;
count = (pos.x+=5);
mover.center = pos;
i want to see object moving in +5 points interval.
and i'm new in objective c and iphone so please help me as fast as possible .
basically my task is move view on the border of the screen mean (left to right ,up to down ,right to left and down to up) means move view in in sequence and through border of the screen of simulator repeatedly.
i hope your answer helpful to me and other who is new in this technology(iPhone).
thank you very much.
To move view from one position to another i would set its frame inside animation block. Something like this would help you:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// `customView` is a UIView defined in .h file
customView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
customView.backgroundColor = [UIColor redColor];
[self.view addSubview:customView];
[self rotateViewInSelfBoundary];
}
-(void)rotateViewInSelfBoundary
{
CGRect screen = [[UIScreen mainScreen] bounds];
// Top Left to top right
[UIView animateWithDuration:3.0f
animations:^{
[customView setFrame:CGRectMake(screen.size.width - customView.frame.size.width, 0, 100, 100)];
}
completion:^(BOOL finished)
{
// Top right to bottom right
[UIView animateWithDuration:3.0f
animations:^{
[customView setFrame:CGRectMake(screen.size.width - customView.frame.size.width, screen.size.height - customView.frame.size.height, 100, 100)];
}
completion:^(BOOL finished)
{
// bottom right to bottom left
[UIView animateWithDuration:3.0f
animations:^{
[customView setFrame:CGRectMake(0, screen.size.height - customView.frame.size.height, 100, 100)];
}
completion:^(BOOL finished)
{
// bottom left to top left
[UIView animateWithDuration:3.0f
animations:^{
[customView setFrame:CGRectMake(0, 0, 100, 100)];
}
completion:^(BOOL finished)
{
// call the same function again.
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(rotateViewInSelfBoundary) object:nil];
[self performSelector:#selector(rotateViewInSelfBoundary) withObject:nil afterDelay:0.0f];
}];
}];
}];
}];
}
Screen shots:
use UIViewAnimationOptionBeginFromCurrentState like
[UIView animateWithDuration:1.0f
delay:2.0f
options:UIViewAnimationOptionRepeat | UIViewAnimationOptionBeginFromCurrentState
animations:^{
//your animation
}
completion:nil];
If you want to move the view in a square, define the four CGPoint values it should animate between and then:
NSArray *values = #[[NSValue valueWithCGPoint:point0],
[NSValue valueWithCGPoint:point1],
[NSValue valueWithCGPoint:point2],
[NSValue valueWithCGPoint:point3],
[NSValue valueWithCGPoint:point0]];
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:#"position"];
animation.values = values;
animation.duration = 5.0;
animation.repeatCount = HUGE_VALF;
[mover.layer addAnimation:animation forKey:#"moveAroundBorder"];
This will take care of all of the moving of the view with no further intervention on your part.
If you want to use the iOS 7 block-based key-frame animation, it would be:
[UIView animateWithDuration:5.0 delay:0.0 options:UIViewAnimationOptionCurveLinear animations:^{
[UIView animateKeyframesWithDuration:5.0 delay:0.0 options:UIViewKeyframeAnimationOptionRepeat | UIViewKeyframeAnimationOptionCalculationModeLinear animations:^{
[UIView addKeyframeWithRelativeStartTime:0.00 relativeDuration:0.25 animations:^{
mover.center = point0;
}];
[UIView addKeyframeWithRelativeStartTime:0.25 relativeDuration:0.25 animations:^{
mover.center = point1;
}];
[UIView addKeyframeWithRelativeStartTime:0.50 relativeDuration:0.25 animations:^{
mover.center = point2;
}];
[UIView addKeyframeWithRelativeStartTime:0.75 relativeDuration:0.25 animations:^{
mover.center = point3;
}];
} completion:nil];
} completion:nil];
Note the curious nesting of animateKeyframesWithDuration within the animateWithDuration. That's because the UIViewKeyframeAnimationOptionCalculationModeLinear curiously does not stop the "ease in / ease out" behavior, so you have to specify UIViewAnimationOptionCurveLinear on the outer animation block.

Transitions between UIView in the same UIViewController

I want to transit from UIView to another in the UIViewController when I press a button
I write this code:
- (IBAction)changeView:(id)sender {
CATransition* transition = [CATransition animation];
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn];
transition.duration = 1.0f;
transition.type = #"cube";
transition.subtype = #"fromTop";
[self.view1.layer removeAllAnimations];
[self.view1.layer addAnimation:transition forKey:kCATransition];
}
this code will transit the view1(UIview) to itself. how can I make it to transit to another UIView (e.g. view2). also where can I put the another UIview (view2) in the storyboard
Have you try like this?
self.view1.alpha = 0.0;
[UIView beginAnimations:#"flip" context:nil];
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:#selector(animationDidStop:finished:context:)];
[UIView setAnimationDuration:1.0f];
self.view2.alpha = 1.0;
[UIView commitAnimations];
Have a look at this code snippet it's much more elegant. Also checkout Apple's documentation for more fancy UIViewAnimationOptions
- (IBAction)changeView:(id)sender
{
[UIView animateWithDuration:1.0 delay:0.0 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
//Remove View-1 from the main view
[view1 removeFromSuperView];
//Add the new view to your current view
[self.view addSubView:view2];
} completion:^(BOOL finished) {
//Once animation is complete
}];
}
Ensure that the two views are a strong property of your view controller.
If you have two UIView for example take firstView and secondView and add it to UIViewController on same rect. and set hiddden property to secondView.
-(IBAction)flipFirstViewToSecondView{
secondView.hidden= NO;
[UIView transitionWithView:firstView duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
[UIView transitionWithView:secondView duration:1.0f options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
} completion:^(BOOL finished) {
secondView.hidden = NO;
firstView.hidden = YES;
}];
} completion:^(BOOL finished) {
}];
}
Thanks. just let me know if you have any problem to integrate it.

Resize view right-to-left with animation on iOS

I would like to resize UIView from right side to left (opposite).
Now I have view with rect like this: CGRectMake(100.f, 0.f, 100.f, 100.f);
[UIView animateWithDuration:0.5 animations:^{
view.frame = CGRectMake(0.f, 0.f, 200.f, 100.f);
}];
The problem is that animation is not nice. It bounces to the right (gets bigger) and than moves to location 0.0,0.0.
I would like to achieve the same effect like when resizing from left to right.
EDIT: code
- (void)resizeSearchBar:(int)type
{
//resize - bigger
if (type == 1) {
[UIView animateWithDuration:1.0 animations:^{
self.searchBar.frame = CGRectMake(0.f, 0.f, 280.f, 44.f);
}];
}
//resize - smaller
else {
[UIView animateWithDuration:1.0 animations:^{
self.searchBar.frame = CGRectMake(95.f, 0.f, 185.f, 44.f);
}];
}
}
EDIT 2
I've also tried to change View properties in IB like that:
Still no luck...
Got it to work with the hint from this answer: Can't animate the frame or bounds of a UISearchBar
The trick is to call [searchBar layoutSubviews]; in your animation block after setting the frame.
Here is what I got to work in my sample:
- (IBAction)resizeButtonTapped:(id)sender {
//resize - bigger
if (searchBar.frame.origin.x == 95) {
[UIView animateWithDuration:1.0 animations:^{
self.searchBar.frame = CGRectMake(0.f, 0.f, 280.f, 44.f);
[searchBar layoutSubviews];
}];
}
//resize - smaller
else {
[UIView animateWithDuration:1.0 animations:^{
self.searchBar.frame = CGRectMake(95.f, 0.f, 185.f, 44.f);
[searchBar layoutSubviews];
}];
}
}
I think this is useful to you just see bellow demo link...
https://github.com/ipup/PPRevealSideViewController?_tmc=UFw1P_Ai9brJd4WEpSlPbNrXBWOvsYZ6gNPi_derQik
i hope this help you..
:)
edited...
[UIView animateWithDuration:0.5
delay:4.0
options: UIViewAnimationOptionTransitionCrossDissolve
animations:nil
completion:^{
view.frame = CGRectMake(0.f, 0.f, 200.f, 100.f);}];
not sure but try this and also use different Animation like "UIViewAnimationOptionTransitionCrossDissolve",etc....
hope this help you....
It bounces because the duration is much less!!
Increase the duration ..and i think it will make it more clear how it animates
K try this
[UIView animateWithDuration:1.0
delay:0.0
options:UIViewAnimationCurveEaseInOut
animations:^ {
self.searchBar.frame = CGRectMake(0.f, 0.f, 280.f, 44.f);
}
completion:^(BOOL finished) {
}];
Chk here for Options

iPhone loading view "slide effect"

I would to achieve that when you request something in my app (like a button pressed or whatever) I would like it to appear a small rectangle that says "loading.." and when the loading is completed it disappear with a "slide up effect". I've made a simple mockups for describing well what I'm trying to achieve. Do you have any hint for me? Thanks.
NOTE: I don't want pull to refresh effect (the loading view appears without the screen scrolling, it appears for example when you send a comment)
UPDATE:
I've tried the Cirrostratus code:
- (IBAction)displayLoadingScreen:(id)sender
{
NSLog(#"pressed");
UIView *loadingView = [[UIView alloc] init];
loadingView.frame = CGRectMake(0,-40,320,40);
[loadingView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:loadingView];
//animate in within some method called when loading starts
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,0,320,40);
}
completion:^(BOOL finished) {
}];
for (int i = 0; i < 10000; i++) {
NSLog(#"%d", i);
}
//animate out within some method called when loading ends
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,-40,320,40);
}
completion:^(BOOL finished){
}];
}
Why the loading view slide in after the for loop? If you try it, before it prints the for loop and after executes the two animations. Ideas?
UPDATE 2
- (void)stopLoading {
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,-40,320,40);
}
completion:^(BOOL finished){
}];
}
- (void)startLoading {
loadingView.frame = CGRectMake(0,-40,320,40);
[loadingView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:loadingView];
//animate in within some method called when loading starts
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,0,320,40);
}
completion:^(BOOL finished) {
}];
sleep(5);
[self stopLoading];
}
- (IBAction)displayLoadingScreen:(id)sender
{
NSLog(#"button pressed");
[self startLoading];
}
loadingView.frame = CGRectMake(0,-40,320,40);
[loadingView setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:loadingView];
//animate in within some method called when loading starts
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,0,320,40);
}
completion:^(BOOL finished){
}];
//animate out within some method called when loading ends
[UIView animateWithDuration:1.0
animations:^{
loadingView.frame = CGRectMake(0,-40,320,40);
}
completion:^(BOOL finished){
}];
What I would do is add a view in front of your other views with the loading design you want.
Then when the loading is finished (use a notification or just call a function of the view), slide it up with that kind of animation :
[UIView animateWithDuration:0.3
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
//change the frame to make the view go out of the screen
}
completion:^(BOOL finished){
//do whatever you want with the view (release it for instance)
}];
you can do it with the help of spinner else you take just view and set animation with its coordinate when you click on any object it will popup n your view and after completion of loading it you have to set '-' coordinate(frame) for your popup view hop it will hekp you......

UIView removeFromSuperview

The removeFromSuperview method actually pulls off the image very quickly, is there a way to remove it in a animated fashion. thanks
If you're targeting iOS4 (code snippet from UIView docs):
[UIView animateWithDuration:0.2
animations:^{ table.alpha = 0.0; }
completion:^(BOOL finished){ [table removeFromSuperview]; }];
code example here
http://www.iphonedevsdk.com/forum/iphone-sdk-development/37467-removefromsuperview-animation-transition.html
1) insert your new subview (index of 0 so it's hidden) onto the view stack.
[window insertSubview:myNewView atIndex:0];
2) Then animation the view out (straight from the docs) is:
[UIView animateWithDuration:0.2
animations:^{ view.alpha = 0.0; }
completion:^(BOOL finished){ [view removeFromSuperview]; }]