2013-04-16 70 views
1

我正在尝试一些如此微不足道的事情!不能相信它不工作!也许是在俯视一些东西 我的iPhone应用程序有一个主页。我想现在添加一个最初不在屏幕上的子视图(即隐藏在右边,即其frame.origin.x将是屏幕宽度),然后我想用触发器将其条目动画化到页面中,水平并部分显示子视图(仅约半屏)。为什么不设置UIView框架?

[self.mySubview.view setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width, 0.0, 
               [[UIScreen mainScreen] bounds].size.width, 
               [[UIScreen mainScreen] bounds].size.height)]; 
[self.view addSubview:self.mySubview.view]; 
NSLog(@"frame before x %f", self.mySubview.view.frame.origin.x); 

[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0]; 
     [self.mySubview.view setFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width*(2/5), 0.0, 
                 [[UIScreen mainScreen] bounds].size.width, 
                 [[UIScreen mainScreen] bounds].size.height)]; 
[UIView commitAnimations]; 
NSLog(@"frame x %f", self.mySubview.view.frame.origin.x); 

正尝试在iPad上运行此。但子视图的框架最初没有设置为768.0。另外,动画没有发生。在动画之前和之后,子视图的frame.origin.x只有0.0。我可以在哪里出错?请帮忙。我尝试了改变故事板中的自动化箭头(删除它们,添加它们)......没有任何东西似乎起作用!

+1

你确定视图层级之前已经运行的代码产生的? **在哪一点**(在控制器的生命周期中)是否执行此代码? – Alladinian

+0

另一个说明你应该使用基于块的动画,而不是beginAnimation和commitAnimation。 – Rakesh

+0

感谢您的意见。我在主页按下按钮时执行此代码。 – Jean

回答

0

试试这个:

instend的

self.mySubview 

self.mySubview.view 
1

尝试这个

Someviewcontroller *c = initWithNibName 
    c.view.frame = CGRectMake(0, 0, 200, 200); 
    [self addSubView:c]; 
相关问题