如何在Xcode中animationUIView的宽度和高度?
我有这个子视图我想添加到我的主视图,但使其从原点扩大。 我读了一些苹果的文档,但我不明白我在哪里犯错。 我可以animation框架的起源,即让它从任何地方滑动,但宽度/高度似乎并不animation。 代码如下:
[UIView beginAnimations:@"animateAddContentView" context:nil]; [UIView setAnimationDuration:0.4]; customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 0, 150); [self.view addSubview:customView]; customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 310, 150); [UIView commitAnimations];
我也尝试把这样的animation只setFrame部分:
customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 0, 150); [self.view addSubview:customView]; [UIView beginAnimations:@"animateAddContentView" context:nil]; [UIView setAnimationDuration:0.4]; customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 310, 150); [UIView commitAnimations];
但是它仍然不起作用!
编辑:
根据这些build议,我已经把它移动到一个基于块的animation解决scheme,这是确切的代码:
NSLog(@"yourview : %@",customView); customView.frame= CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 0, 150); NSLog(@"yourview : %@",customView); [self.view addSubview:customView]; NSLog(@"yourview : %@",customView); // [customView setFrame:CGRectMake( 0.0f, 480.0f, customView.frame.size.width, customView.frame.size.height)]; [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationCurveEaseInOut animations:^ { NSLog(@"yourview : %@",customView); customView.frame = CGRectMake(self.view.frame.origin.x +5, self.view.frame.origin.y +5, 310, 150); NSLog(@"yourview : %@",customView); }completion:^(BOOL finished) { }];
由于某种原因,这仍然不起作用。 我看到的可能的问题是:
- 我从一个
nib
加载这个自定义视图,特别是310由150,也许这是造成一个问题。 - 我没有导入正确的框架,但因为我可以animationframe.origin这个视图的一部分,我不知道是这样的…我有
QuartzCore
和Core Graphics
全部导入和东西。
在日志中的每一个点都给出了正确的东西:例如,在目标框架的大小为0,150之前,以及我将框架设置为310,150之后。但是animation不起作用!
为了使视图“成长”到位,不要使框架animation化。 animation变换。
从笔尖加载你的子视图。 将其转换设置为0:
view.transform = CGAffineTransformMakeScale(0,0);
然后将其添加到您的超级视图。
在animation块中,将变换设置为identity:
view.transform = CGAffineTransformIdentity;
这个观点将会变得正常。 您可能需要摆弄锚点才能从正确的angular度发展。
您也可以更改块内的框架,但仅移动视图我更喜欢更改中心属性,如果还有变换,则不应尝试设置框架。
对于奖励积分,还可以animation到略大于1.0的比例,然后在完成块中以链接animation回到身份变换。 这使得视图像一个警报视图一样“popup”在屏幕之外。
尝试使用块将会更清楚。
// First create the view if you haven't already UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)]; // Add it as a subview. [myViewController.view addSubview:myView]; CGRect newFrameOfMyView = CGRectMake(0.0f, 0.0f, 200.0f, 200.0f); /* * Alternatively you could just set the width/height or whatever you'd like with this: * CGRect newFrameOfMyView = myView.frame; * newFrameOfMyView.size.height = 200.0f; * newFrameOfMyView.size.width = 200.0f; */ [UIView animateWithDuration:0.3f animations:^{ myView.frame = newFrameOfMyView; } completion:^(BOOL finished){ NSLog( @"woo! Finished animating the frame of myView!" ); }];
尝试这个:
customView.frame= CGRectMake(self.view.frame.origin.x +5,self.view.frame.origin.y+5,0, 150); [self.view addSubview:customView]; CGRect frame = customView.frame; frame.size.width = 310; [UIView animateWithDuration:0.4 animations:^{ customView.frame= frame; }];
或者,如果您想使用beginAnimation方法而不是block Methos,请使用以下命令:
UIView *customView=[[UIView alloc]initWithFrame:CGRectMake(self.view.frame.origin.x +5,self.view.frame.origin.y+5,0, 150)]; [self.view addSubview:customView]; CGRect frame = customView.frame; frame.size.width = 310; [UIView beginAnimations:@"animateAddContentView" context:nil]; [UIView setAnimationDuration:0.4]; customView.frame= frame; [UIView commitAnimations];
重要的一点
1)在animation之前将视图添加到您的父视图,并以起始帧。
2)然后只给animation块内的目标帧
你不再需要这个,因为你是从nib添加视图。
//UIVIew yourView = [[UIView alloc] initWithFrame:YOUR_STARTING_FRAME]; //[self.view addSubview:yourView]; ......... ......... .........
只要给你的视图上的animation代码..确保你的视图不是零(似乎是这样的话..)
NSLog(@"yourview : %@",yourView); [UIView animateWithDuration:your_duration delay:your_starting_delay options:UIViewAnimationCurveEaseInOut animations:^ { yourView.frame = YOUR_TARGET_FRAME; }completion:^(BOOL finished) { }];
这是一个有趣的方式来animation您的意见。 在我的例子中,我有一个触摸动作,将执行块animation和一个布尔,以确保重置视图回到原来的状态。
首先你把2个UIViews放到一个UIViewController上(你可以用不同的颜色来对比它们)。 将它们连接到您创build的“MainViewController.h”文件以控制大视图。 .h文件应该如下所示:
#import <UIKit/UIKit.h> @interface MainViewController : UIViewController @property (weak, nonatomic) IBOutlet UIView *topView; @property (weak, nonatomic) IBOutlet UIView *bottomView; @end
和你的.m文件应该是这样的:
#import "MainViewController.h" #define kViewAnimateWithDuration 0.35f #define kViewDelay 0.05f @interface MainViewController () @property (nonatomic) BOOL setTouch; @end @implementation MainViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization self.setTouch = NO; } return self; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [touches anyObject]; CGPoint touchLocation = [touch locationInView:self.view]; float width_tp = self.topView.frame.size.width; float height_tp = self.topView.frame.size.height; float width_b = self.bottomView.frame.size.width; float height_b = self.bottomView.frame.size.height; if ((CGRectContainsPoint(self.bottomView.frame, touchLocation)) && !self.setTouch){ [UIView animateWithDuration:kViewAnimateWithDuration delay:kViewDelay options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{ //Move frame or transform view [self.topView setFrame:CGRectMake(self.topView.frame.origin.x, self.topView.frame.origin.y, width_tp, height_tp + 171)]; [self.bottomView setFrame:CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y +171, width_b, height_b -171)]; } completion:^(BOOL finished) { self.setTouch = YES; }]; } if ((CGRectContainsPoint(self.bottomView.frame, touchLocation)) && self.setTouch) { [UIView animateWithDuration:kViewAnimateWithDuration delay:kViewDelay options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction animations:^{ //Move frame or transform view [self.topView setFrame:CGRectMake(self.topView.frame.origin.x, self.topView.frame.origin.y, width_tp, height_tp - 171)]; [self.bottomView setFrame:CGRectMake(self.bottomView.frame.origin.x, self.bottomView.frame.origin.y -171, width_b, height_b +171)]; } completion:^(BOOL finished) { self.setTouch = NO; }]; } }
所以只要确保topView有像x:0,y:0宽度:320高度:43和bottomView像x:0 y:40宽度:320高度:528一些小尺寸。 记得给背景涂上不同的颜色。 然后运行程序,你应该看到animation。
对我来说,我认为在代码中更重要的行之一是setFrame:对我来说,使框架自动重新显示矩形,而不必使用DrawRect方法。 设置属性会相应地更改中心属性指定的点和边界矩形中的大小。
当然,还有其他有趣的方法可以做到这一点,但我希望这可以帮助有人让iOS看起来很神奇! 愉快的旅程!
如果你的viewcontrollers viewWillLayoutSubviews(或者这样)设置了你的视图的“初始”框架,那么在animation循环过程中显然会被调用,因此会重置由animation设置的框架。