【UIKit-124-3】#import <UIKit/UIView.h>

本文详细介绍了UIKit中UIView的绘制方法与属性设置,包括重绘、裁剪、填充方式及透明度等核心概念,并提供了示例代码帮助理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


【视图渲染】

@interface UIView(UIViewRendering)


- (void)drawRect:(CGRect)rect; // 重绘


- (void)setNeedsDisplay; // 标记需要展示

- (void)setNeedsDisplayInRect:(CGRect)rect;// 展示



【裁剪】

@property(nonatomic)                 BOOL              clipsToBounds;              // 父视图,对于超出自身范围的子视图进行裁剪 (默认NO)

- (void)viewDidLoad {
    [super viewDidLoad];
    
    redView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    
    greenView = [[UIView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
    greenView.backgroundColor = [UIColor greenColor];
    [redView addSubview:greenView]; 
    
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    redView.clipsToBounds = YES;
}


【一般属性】

@property(nonatomic,copy)            UIColor          *backgroundColor;  // 背景

@property(nonatomic)                 CGFloat           alpha;                   // 透明度

@property(nonatomic,getter=isOpaque) BOOL              opaque;        // 默认不透明,不计算透明时的重叠复杂计算。(只在drawRect中使用?)

@property(nonatomic)                 BOOL              clearsContextBeforeDrawing; // 默认YES,绘制前,清空

@property(nonatomic,getter=isHidden) BOOL              hidden;                     // 隐藏,包括子视图





【填充方式】

@property(nonatomic)                 UIViewContentMode contentMode;                // 填充方式,一般是imgView

    

    UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 300, 150, 150)];
    imgView.image = [UIImage imageNamed:@"dadajie_chenjie"];
    imgView.backgroundColor = [UIColor redColor];
    [self.view addSubview:imgView];
    
    imgView.contentMode = UIViewContentModeRedraw;
    
    /*
     typedef NS_ENUM(NSInteger, UIViewContentMode) {
     UIViewContentModeScaleToFill,          imgView比例,填充图片
     UIViewContentModeScaleAspectFit,       图片比例,填充imgView(不满处透明)
     UIViewContentModeScaleAspectFill,      图片比例,填充imgView(不满处白色)
     UIViewContentModeRedraw,               与第一个很像,(setNeedsDisplay?)
     
     //下面的图片都是原来大小,对其方式与imgView比较
     UIViewContentModeCenter,
     UIViewContentModeTop,
     UIViewContentModeBottom,
     UIViewContentModeLeft,
     UIViewContentModeRight,
     UIViewContentModeTopLeft,
     UIViewContentModeTopRight,
     UIViewContentModeBottomLeft,
     UIViewContentModeBottomRight,
     };
     */





【tintColor】

// 与系统有关的一些颜色的修改。待研究。

@property(nonatomic,retain) UIColor *tintColor NS_AVAILABLE_IOS(7_0);

@property(nonatomic) UIViewTintAdjustmentMode tintAdjustmentMode NS_AVAILABLE_IOS(7_0);

- (void)tintColorDidChange NS_AVAILABLE_IOS(7_0);

    /*

     typedef NS_ENUM(NSInteger, UIViewTintAdjustmentMode) {

     UIViewTintAdjustmentModeAutomatic,

     

     UIViewTintAdjustmentModeNormal,

     UIViewTintAdjustmentModeDimmed,

     };

     

     */



@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值