您的位置:首页 > 移动开发 > IOS开发

iOS masonry动态约束控件位置

2015-07-01 10:17 435 查看
#import "FourViewController.h"

#import "View+MASAdditions.h"

#import "FiveViewControllerr.h"

#define WS(weakSelf) __weak __typeof(&*self)weakSelf = self;

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

static NSString const*kStoryboardName=@"FourViewController";

static NSString const*kIdentifier=@"FourViewController";

@interface FourViewController ()

@property (strong, nonatomic) UIButton *move;

@property (strong, nonatomic) UIButton *back;

@property (strong, nonatomic) UIImageView *imageView;

@property (assign, nonatomic) int s;

@end

@implementation FourViewController

+ (instancetype)createViewControllerWithStoryboard:(id)createArgs{

UIStoryboard *storyboard=[UIStoryboard storyboardWithName:(NSString *)kStoryboardName bundle:nil];

FourViewController *vc=[storyboard instantiateViewControllerWithIdentifier:(NSString *)kIdentifier];

return vc;

}

- (void)viewDidLoad {

[super viewDidLoad];

[self initRightItem];

_s=10;

[self initFirstContraints];

}

- (void)initFirstContraints{

//WS(ws);

// UIView *view=[UIView new];

// view.backgroundColor=[UIColor brownColor];

// [self.view addSubview:view];

// [view mas_makeConstraints:^(MASConstraintMaker *make) {

// make.left.equalTo(self.view).with.offset(0);

// make.size.mas_equalTo(CGSizeMake([UIScreen mainScreen].bounds.size.width,[UIScreen mainScreen].bounds.size.height));

// }];

//int width=([UIScreen mainScreen].bounds.size.width-_s*2-10)/2;

_move=[[UIButton alloc]init];

_move.backgroundColor=[UIColor blueColor];

[self.view addSubview:_move];

[_move mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.view).with.offset(_s);

make.top.equalTo(self.view).with.offset(100);

make.width.equalTo(@150);

make.height.equalTo(@40);

}];

[_move addTarget:self action:@selector(moveFrame) forControlEvents:UIControlEventTouchUpInside];

[_move setTitle:@"移动图片" forState:UIControlStateNormal];

_back=[[UIButton alloc]init];

_back.backgroundColor=[UIColor greenColor];

[self.view addSubview:_back];

[_back mas_makeConstraints:^(MASConstraintMaker *make) {

make.right.equalTo(self.view).with.offset(-_s);

make.top.equalTo(self.view).with.offset(100);

make.width.equalTo(@150);

make.height.equalTo(@40);

}];

[_back addTarget:self action:@selector(backFrame) forControlEvents:UIControlEventTouchUpInside];

[_back setTitle:@"图片回来" forState:UIControlStateNormal];

_imageView=[[UIImageView alloc]init];

_imageView.image=[UIImage imageNamed:@"move"];

[self.view addSubview:_imageView];

[_imageView mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.view).with.offset(_s);

make.top.equalTo(self.view).with.offset(200);

make.width.equalTo(@150);

make.height.equalTo(@150);

}];

}

- (void)moveFrame{

_s=_s+5;;

NSLog(@"_s:%d",_s);

[_imageView mas_updateConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.view).with.offset(_s);

make.top.equalTo(self.view).with.offset(200+_s);

make.width.equalTo(@150);

make.height.equalTo(@150);

}];

}

- (void)backFrame{

_s=_s-5;;

NSLog(@"_s:%d",_s);

[_imageView mas_updateConstraints:^(MASConstraintMaker *make) {

make.left.equalTo(self.view).with.offset(_s);

make.top.equalTo(self.view).with.offset(200-_s);

make.width.equalTo(@150);

make.height.equalTo(@150);

}];

}

- (void)initRightItem{

[self setTitle:@"动态改变控件位置"];

UIBarButtonItem *right=[[UIBarButtonItem alloc]initWithTitle:@"多约束" style:UIBarButtonItemStylePlain target:self action:@selector(right)];

self.navigationItem.rightBarButtonItem=right;

}

- (void)right{

FiveViewControllerr *vc=[FiveViewControllerr createViewControllerWithStoryboard:nil];

[self.navigationController pushViewController:vc animated:YES];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: