iOS-setNeedsDisplay和layoutSubViews

两个方法都是异步执行的。而setNeedsDisplay会调用自动调用drawRect方法,这样可以拿到UIGraphicsGetCurrentContext,就可以画画了。而setNeedsLayout会默认调用 layoutSubViews,就可以处理子视图中的一些数据。

综上所述,setNeedsDisplay方便绘图,而layoutSubViews方便出来数据。

当UIView设置为自动适配屏幕时,当用户旋转设备的时候,会调用layoutSubviews方法,我们只需重写 这个方法,然后判断用户屏幕的方向。在调整每个空间的位置即可。

  • 在ContentView中重写layoutSubviews方法,然后根据stausbar的方向判断当前视图的横竖屏。具体代码:

    -(void)layoutSubviews{ 
        [super layoutSubviews]; 
        UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation]; 
        if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) { 
            //翻转为竖屏时 
            [self setVerticalFrame]; 
        }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) { 
            //翻转为横屏时 
            [self setHorizontalFrame]; 
        } 
    }

    -(void)setVerticalFrame 

        NSLog(@"竖屏"); 
        [titleLable setFrame:CGRectMake(283, 0, 239, 83)]; 
        [leftView setFrame:CGRectMake(38, 102, 384, 272)]; 
        [rightView setFrame:CGRectMake(450, 102, 282, 198)]; 
    }

    -(void)setHorizontalFrame 

        NSLog(@"横屏"); 
        [titleLable setFrame:CGRectMake(183, 0, 239, 83)]; 
        [leftView setFrame:CGRectMake(168, 122, 384, 272)]; 
        [rightView setFrame:CGRectMake(650, 122, 282, 198)]; 
    }

    在具体的横竖屏方法中,从新设置各个组件的坐标即可。

  • 接下来在ContentView中添加ArticleView视图。

    -(id)initWithCoder:(NSCoder *)aDecoder 

        if ((self = [super initWithCoder:aDecoder])) {

            NSArray *arrayContentView =[[NSBundle mainBundle] loadNibNamed:@"ArticleView" owner:self options:nil]; 
            rightView=[arrayContentView objectAtIndex:0]; 
            [self addSubview:rightView]; 
        } 
        return self; 
    }

由于我用的是xib,所以初始化方法为initWithCoder,在这个中添加新的视图。

同样在ArticleView中设置横竖屏相应空间的坐标即可。

-(void)layoutSubviews{ 
    [super layoutSubviews]; 
    UIDeviceOrientation interfaceOrientation=[[UIApplication sharedApplication] statusBarOrientation]; 
    CGRect rect=self.frame; 
    rect.size.width=282; 
    rect.size.height=198; 
    [self setFrame:rect]; 
    if (interfaceOrientation == UIDeviceOrientationPortrait || interfaceOrientation == UIDeviceOrientationPortraitUpsideDown) { 
        //翻转为竖屏时 
        [self setVerticalFrame]; 
    }else if (interfaceOrientation==UIDeviceOrientationLandscapeLeft || interfaceOrientation == UIDeviceOrientationLandscapeRight) { 
        //翻转为横屏时 
        [self setHorizontalFrame]; 
    } 
}

-(void)setVerticalFrame 

    NSLog(@"竖屏"); 
    [contentView setFrame:CGRectMake(12, 6, 250, 125)]; 
    [textLable setFrame:CGRectMake(50, 139, 182, 39)]; 
}

-(void)setHorizontalFrame 

    NSLog(@"横屏"); 
    [contentView setFrame:CGRectMake(12, 6, 106, 158)]; 
    [textLable setFrame:CGRectMake(135, 11, 147, 39)]; 
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值