简体   繁体   中英

iPhone Animation : How to animate only subview ? Not the whole view?

I am new with animations and I am using following code for Flip animation.

//MyMapView is UIView

MyMapView *aMapView = [[MyMapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300) AndResArray:self.nearMeArray];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self.view cache:YES];
    [[self view] addSubview:nearMeMap];

    [UIView commitAnimations];

Its working fine. But whole view (with super view) Flips.

I only want to Flip subview that is MyMapView . The super view should not flip.

How to do this ???

EDITED

screen :

Below is main screen. When click on Map button I want to Flip only Brown part of the screen. Whole screen should not Flip.

在此输入图像描述

Thanks...

I have figure out what's the problem. Please see the below code.

MapView.m

- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:frame];
if (self) {
    // Initialization code.

    [self performSelector:@selector(addMapView) withObject:nil afterDelay:2.0];

}
return self;
}

- (void) addMapView {

//MyMapView is UIView

MKMapView  *aMapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 321, 300)];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:self cache:YES];
[self addSubview:aMapView];

[UIView commitAnimations];

}

And after that simply add object of MapView class where you want. like below.

//MyMapView is UIView
mapView  *aMapView = [[mapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300)];
[[self view] addSubview:aMapView];

vote up if this comment help you.

Thanks,

MinuMaster.

Matteo's answer is partially correct, but you need to set the animation on the view before you add it, or it won't work correctly. Like this:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
                       forView:aMapView
                         cache:YES];
[self.view addSubview:aMapView];
[UIView commitAnimations];

Alternatively, you can use the block-based API introduced in iOS 4. Here's an example.

[UIView transitionWithView:aMapView
        duration:1.0
        options:UIViewAnimationTransitionFlipFromRight
        animations:^{ [self.view addSubview:aMapView]; }
        completion:^(BOOL f){ }
];

You need to add your view to self.view and then flip only aMapView:

MyMapView *aMapView = [[MyMapView alloc] initWithFrame:CGRectMake(0, 118, 321, 300) AndResArray:self.nearMeArray];

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1.0];
[self.view addSubView:aMapView];
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight forView:aMapView cache:YES];

[UIView commitAnimations];

Animated transitions like this animate the contents of a view. The system takes a snapshot of the contents at the beginning of the animation, another snapshot when you commit, and then animates between them. As you've seen with your original code, the entire view gets animated.

The simplest solution is probably just to introduce an intermediate view that's the size and position of where the map will go. Then animate that view as you add the map to it.

More info in the View Programming Guide's section on Creating Animated Transitions Between Views . Though the methods you're using have, in iOS 4, been superceded by new block-based versions.

-(void)viewDidLoad
{
    [super viewDidLoad];

    OptionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 70, 70)];
    OptionView.backgroundColor=[UIColor blueColor];
    OptionIsShow=false;

    MoveView=[[UIView alloc] initWithFrame:CGRectMake(40, 40, 70, 70)];
    [MoveView setBackgroundColor:[UIColor redColor]];
    [self.view addSubview:MoveView];

}

-(void)ViewOrHideOptionView
{

        CGContextRef context = UIGraphicsGetCurrentContext();
        [UIView beginAnimations:nil context:context];

        [UIView setAnimationTransition: UIViewAnimationTransitionFlipFromLeft forView:self.MoveView cache:NO];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDuration:1.0];

        if(!OptionIsShow)
        {
            [self.MoveView addSubview:OptionView];
            OptionIsShow=true;
        }
        else
        {
            [self.OptionView removeFromSuperview];
            OptionIsShow=false;
        }

        [UIView commitAnimations];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM