Cause
When used on a certain day DKChainableAnimationKit
, you can write the following code:
view.animation.rotate(180).anchorTopLeft.thenAfter(1.0).rotate(90).anchorCenter.animanimation
Coincidentally. Masonry
actually, it's the same thing.
make.right.equalTo(self.view).with.offset(-10);
Principle
Look at the code, in fact, through a realization of all methods Chaining Method Object
, each method will return a ' Block
, 这个
返回类型为
the parameters for block Chaining Method Object ,
block are the type of arguments you want to pass in.
@interfaceChainingmethodobject:nsobject-(Chainingmethodobject * (^) (void)) doA;-( Chainingmethodobject * (^) (nsinteger i)) dob;-(Chainingmethodobject * (^) (nsstring* str") doc;-(Chainingmethodobject * (^) (nsstring* str, nsarray* Array)) DoD; @end @implementation chainingmethodobject-(Chainingmethodobject * (^) (NSInteger i)) doB{return ^id (NSInteger i) {//do actual stuff related with B return self;}; ... Other methods like @end
Usually, there ChainingMethodObject
will be, delegate
depending on the actual use of the situation, such as the animation library DKChainableAnimationKit
, animation
there is a weak var view:UIView
pointer to the UIView so that the target view operation.
@implementation ChainingMethodObject- (id) initWithObject:(id)obj{ self = [super init]; _delegate = obj; return self;}@end
@interface HostObject()ChainingMethodObject * _cObj;@end@implementation HostObject (ChainingMethodObject)- (ChainingMethodObject *) getChainingMethodObject{ if (!_cObj) _cObj = [[ChainingMethodObject alloc] initWithObject:self]; return _cObj;}@end
Then you can:
HostObject* hostObject = [HostObject new];[hostOjbect getChainingMethodObject].doA.doC(@"Hi there!").doD(@"Hello",@[@1,@2]).doB(100).doA;
Reference
Dkchainableanimationkit
Masonry
Objective C Chaining calls