繁体   English   中英

iPhone动画:如何仅为子视图制作动画?不是整个观点?

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

我是动画的新手,我正在使用以下代码进行翻转动画。

//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];

它的工作正常。 但整个视图(超级视图)翻转。

我只想MyMapView子视图。 超级视图不应该翻转。

这该怎么做 ???

EDITED

屏幕:

下面是主屏幕。 当点击地图按钮时,我想翻转屏幕的棕色部分。 整个屏幕不应该翻转。

在此输入图像描述

谢谢...

我弄清楚问题是什么。 请参阅以下代码。

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];

}

之后,只需添加MapView类的对象即可。 如下。

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

如果此评论对您有帮助,请立即投票

谢谢,

MinuMaster。

Matteo的答案部分正确,但您需要在添加动画之前在视图上设置动画,否则它将无法正常工作。 像这样:

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

或者,您可以使用iOS 4中引入的基于块的API。这是一个示例。

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

您需要将视图添加到self.view,然后只翻转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];

像这样的动画过渡动画视图的内容。 系统会在动画开始时拍摄内容的快照,在提交时拍摄另一个快照,然后在它们之间设置动画。 正如您在原始代码中看到的那样,整个视图都会生成动画。

最简单的解决方案可能只是引入一个中间视图,它是地图的大小和位置。 然后在将地图添加到视图时为该视图设置动画。

有关在视图之间创建动画过渡的视图编程指南的部分中的更多信息。 虽然您使用的方法在iOS 4中已被新的基于块的版本取代。

-(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];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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