2012-05-30 15 views
1

我的代码是这样的:如何在定向发生时刷新UIView?

- (void)viewDidLoad 
{ 
    [self setGridView]; 
} 
-(void)setGridView 
{ 
    CGRect frame; 
    frame .origin.x=0; 
    frame.origin.y=20; 
    frame.size.width=GRID_WEIGHT; 
    frame.size.height=GRID_HEIGHT; 
    GridView *ObjGridView=[[GridView alloc]initWithFrame:frame]; 

    [[NSBundle mainBundle ] loadNibNamed:@"GridView" owner:ObjGridView options:nil]; 
    [ObjGridView setGridViewFrame:frame]; 

    [self.view addSubview:ObjGridView.GridCellView]; 
    frame .origin.x+=GRID_WEIGHT; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return YES; 
} 

该代码添加一个子视图的视图,并设置帧

我的问题: 1 - 如何刷新我的看法时,方向(横向或纵向)发生, ,因为我在lanscape模式下设置子视图的框架,我也想在我的肖像视图中使用理智的视图(基本上我在哪里调用 - (void)setGridView委托方法)?

2我如何知道,我的子视图超出了视图的边界,以便我可以在我的setGridView方法中处理子视图?

回答

2

1.当方向改变时,下面的方法会自动调用。根据每个方向进行必要的更改。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 

if (interfaceOrientation == UIInterfaceOrientationPortrait) {} 

else if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) {} 

else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight) {} 

else if (interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown) {} 

return YES; 

} 

2.您应该知道您的视图的宽度和高度,并相应地设置框架。这不是什么大不了的事。

希望这是有帮助的。

+0

是的,朋友它没有.. –

1

我正在学习iOS应用程序开发的细节,所以请原谅我的简短回应。

我相信你可以找到名为苹果的开发者资源本文档中的“应对方向变化”的部分中回答了你的问题:

http://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/RespondingtoDeviceOrientationChanges/RespondingtoDeviceOrientationChanges.html

我希望这可以帮助你演绎解决您的问题。

+0

任何机构可以建议我说,我怎么知道,我的子视图超出边界的观点,这样我可以亨德尔我的方法的子视图? –

+0

帮我,大家好,我的上述问题 –

+0

检查下面我的职务。 –

0

在回答您的questionsL

关于调整的方向变化: 如果设置的弹簧和相应的支柱它会自动自动调整,或者你可以在代码中这样做,因为每deamonsarea的答案

要检查视图是否超出了超级视图的范围,请使用CGRectContainsRect 之类的东西。

CGRect frame0 = self.view.bounds; 
CGRect frame1 = ObjGridView.frame; 
if(CGRectContainsRect(frame0,frame1)==NO){ 
    NSLog(@"exceeds bounds") 
} 

也注意到你是不是调用[超级viewDidLoad中],并将该行

[[NSBundle mainBundle ] loadNibNamed:@"GridView" owner:ObjGridView options:nil]; 

负载视图的新实例,但你是不是指的它的任何地方

0

我发现这个问题同时寻找一种方式来对UIView本身内的方向变化做出反应。如果有其他人出现...

如果您想对方向变化反应的UIView内,而不是UIViewController(用于封装或其他原因),您可以使用此方法:

class MyView: UIView { 
    override func layoutSubviews() { 
    super.layoutSubviews() 
    println("orientation or other bounds-impacting change") 
    } 
} 
1

viewDidLoad

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(OrientationChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; 

对于方法通知您,朝向变为: -

-(void)OrientationChange:(NSNotification*)notification 
{ 
    UIDeviceOrientation Orientation=[[UIDevice currentDevice]orientation]; 

    if(Orientation==UIDeviceOrientationLandscapeLeft || Orientation==UIDeviceOrientationLandscapeRight) 
    { 
     NSLog(@"Landscape"); 
    } 
    else if(Orientation==UIDeviceOrientationPortrait) 
    { 
     NSLog(@"Portrait"); 
    } 
}