Masonry
I would like to retrieve all items I would like to retrieve. I would like to retrieve all items I would like to retrieve.
[titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.right.top.bottom.equalTo(10);
}];
Copy the code
Let’s focus only on the syntax here, which can be used without limit, and take a look at the internal implementation of the mas_makeConstraints method here:
- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block { self.translatesAutoresizingMaskIntoConstraints = NO; MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self]; block(constraintMaker); return [constraintMaker install]; }Copy the code
We can think of MASConstraintMaker as a layout maker, and what [constraintMaker Install] returns is an array of type NSArray. We won’t go into further code. We’ll just look at the element MASConstraint in this array. A MASConstraint is a constraint. Multiple constraints combine to determine the size of a control’s position, so let’s look at the ones we use most often
- (MASConstraint *)left;
- (MASConstraint *)top;
- (MASConstraint *)right;
- (MASConstraint *)bottom;
- (MASConstraint *)width;
- (MASConstraint *)height;
- (MASConstraint * (^)(id attr))equalTo;
Copy the code
Here we focus on equalTo, because equalTo is a call, so its return value must be a block, plus equalTo needs to be passed, so it is defined as
- (MASConstraint * (^)(id attr))equalTo;
Copy the code
Because the block it returns is MASConstraint itself, it can still make point-by-point calls. That’s the idea of chained programming.
- Note: The idea of chained programming is specific: the method return value must have a method caller;
Chain programming
For example, I’ll link you to the next page and I’ll link it to the next page. For example, I’ll post a call to mas_makeConstraints, which would be a category method. Now let’s create a class of NSObject, and we’ll call it
@interface NSObject (Calculate)
+ (NSInteger)bx_calculate:(void(^)(CalculateManager *manager))block;
@end
@implementation NSObject (Calculate)
+ (NSInteger)bx_calculate:(void(^)(CalculateManager *manager))block{
CalculateManager *manager = [[CalculateManager alloc] init];
block(manager);
return manager.result;
}
@end
Copy the code
Here CalculateManager is equivalent to MASConstraintMaker is a calculation management class, so we define an addition in the CalculateManager class
// Compute Manager@interface CalculateManager: nsobject@property (nonatomic, assign) NSInteger result; // Calculate result // add - (CalculateManager *(^)(NSInteger value))add; The @end@implementation CalculateManager //add block method passes in parameters, The return value needs to be CalculateManager to continue using the add method - (CalculateManager *(^)(NSInteger value))add{return ^(NSInteger value){_result += value; return self; }; } @endCopy the code
This allows for infinite calls in chained programming
NSInteger result = [NSObject bx_calculate:^(CalculateManager *manager) {
manager.add(5).add(10).add(20);
}];
NSLog(@"---->%d",result);
Copy the code
Running results:
- Write an article for the first time, have what wrong hope to point out, thank you