@interface ViewController ()
@property (strong, nonatomic) UIView *containerView
@property (strong, nonatomic) UILabel *logLabel
@property (nonatomic, strong) UILabel *tipLabel
@property (nonatomic, strong) MASConstraint *leftConstraint
@property (nonatomic, strong) MASConstraint *topConstraint
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad]
[self.view addSubview:self.containerView]
[self.view addSubview:self.logLabel]
[_containerView addSubview:self.tipLabel]
[self p_addMasonry]
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panWithGesture:)]
[_containerView addGestureRecognizer:pan]
}
- (void)p_addMasonry {
[_tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
// 设置边界条件约束,保证内容可见,优先级1000
make.left.mas_greaterThanOrEqualTo(_containerView.mas_left);
make.right.mas_lessThanOrEqualTo(_containerView.mas_right);
make.top.mas_greaterThanOrEqualTo(_containerView.mas_top);
make.bottom.mas_lessThanOrEqualTo(_containerView.mas_bottom);
_leftConstraint = make.centerX.equalTo(_containerView.mas_left).mas_offset(50).priorityHigh(); // 优先级要比边界条件低
_topConstraint = make.centerY.equalTo(_containerView.mas_top).mas_offset(50).priorityHigh(); // 优先级要比边界条件低
make.width.mas_equalTo(CGRectGetWidth(_tipLabel.frame) + 8);
make.height.mas_equalTo(CGRectGetHeight(_tipLabel.frame) + 4);
}]
}
- (void)panWithGesture:(UIPanGestureRecognizer *)pan {
CGPoint touchPoint = [pan locationInView:_containerView]
_logLabel.text = NSStringFromCGPoint(touchPoint)
_leftConstraint.offset = touchPoint.x
_topConstraint.offset = touchPoint.y
}
- (UIView *)containerView {
if (!_containerView) {
_containerView = [[UIView alloc] init]
_containerView.layer.masksToBounds = YES
_containerView.layer.borderWidth = 1.0f
_containerView.layer.borderColor = [UIColor redColor].CGColor
_containerView.frame = CGRectMake(15, 100, [UIScreen mainScreen].bounds.size.width - 30, [UIScreen mainScreen].bounds.size.width - 30)
}
return _containerView
}
- (UILabel *)tipLabel {
if (!_tipLabel) {
_tipLabel = [[UILabel alloc] init]
_tipLabel.textColor = [UIColor whiteColor]
_tipLabel.font = [UIFont systemFontOfSize:20]
_tipLabel.numberOfLines = 2
_tipLabel.textAlignment = NSTextAlignmentCenter
_tipLabel.backgroundColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:1]
_tipLabel.layer.borderColor = [UIColor blackColor].CGColor
_tipLabel.layer.borderWidth = 1.0f
_tipLabel.layer.masksToBounds = YES
_tipLabel.layer.cornerRadius = 2.0f
_tipLabel.text = @"tutuge.me\niOS"
[_tipLabel sizeToFit]
}
return _tipLabel
}
- (UILabel *)logLabel {
if (!_logLabel) {
_logLabel = [[UILabel alloc] init]
_logLabel.textColor = [UIColor whiteColor]
_logLabel.font = [UIFont systemFontOfSize:20]
_logLabel.numberOfLines = 2
_logLabel.textAlignment = NSTextAlignmentCenter
_logLabel.backgroundColor = [UIColor colorWithRed:0.39 green:0.39 blue:0.39 alpha:1]
_logLabel.layer.borderColor = [UIColor blackColor].CGColor
_logLabel.layer.borderWidth = 1.0f
_logLabel.layer.masksToBounds = YES
_logLabel.layer.cornerRadius = 2.0f
_logLabel.frame = CGRectMake(0, CGRectGetMaxY(_containerView.frame) + 10, [UIScreen mainScreen].bounds.size.width, 40)
}
return _logLabel
}
@end
