美文网首页
2.1快速修改frame

2.1快速修改frame

作者: 草根小强 | 来源:发表于2019-04-08 16:18 被阅读0次
#import "ViewController.h"
#import "UIView+FastFrame.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self testFastFrame];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)testFastFrame{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    label.backgroundColor = [UIColor redColor];
    
    //设置x坐标
    [label setX:200];
    //贴到屏幕上做显示
    
    [self.view addSubview:label];
    
    
    
}
@end
#import <UIKit/UIKit.h>

@interface UIView (FastFrame)

//- (void)setX:(CGFloat)x;
//- (CGFloat)x;

#warning 注意,在这里,我们没有写成成员变量,下面的这个property只是帮我们去生成了 setX 和getx方法


@property (nonatomic,assign) CGFloat x;

@end
#import "UIView+FastFrame.h"
// 这里用类别给UIview 添加一个方法
@implementation UIView (FastFrame)
- (void)setX:(CGFloat)x{
//    CGFloat _x;
//    _x = x;
    // 修改一个视图的x
    //1.拿出原来的frame
    CGRect frame = self.frame;
    //2.修改
    frame.origin.x = x;
    //替换回去
    self.frame = frame;
    
}

- (CGFloat)x{
    
    return   self.frame.origin.x;

}

@end

相关文章

网友评论

      本文标题:2.1快速修改frame

      本文链接:https://www.haomeiwen.com/subject/tahpiqtx.html