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];
}
#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];
}
相关文章推荐
- iOS Crash收集框架
- cisco 交换机恢复IOS与更改波特率
- 使用Xcode和Instruments调试解决iOS内存泄露
- ios支付宝支付开发
- iOS项目添加CocoaPods
- IOS上如何画出1像素的线
- AFNetWorking在IOS9 请求数据失败解决方案
- IOS字符转义 URL encode 的方法
- iOS开发之多表视图滑动切换示例(仿"头条"客户端)
- iOS开发之Relaese时关闭NSLog使之不在控制台显示
- 开始iOS 7中自动布局教程
- [Mac] Mac OS/iOS/XCode版本历史
- iOS 真机调试(最具体的步骤来解决历史,hmt精心打造)
- ZZCustomAlertView - 一个高度自定义的iOS模态弹窗
- ZZCustomAlertView - 一个高度自定义的iOS模态弹窗
- iOS蓝牙编程
- 获取iOS设备信息
- 使用plist固化iOS应用数据
- IOS RUNTIME笔记
- IOS OC 对象内部尽量直接访问实例变量