BaseControl button collection and basecontrol collection
BaseControl button collection
Effect
Source code
Https://github.com/YouXianMing/Animations
/// POPBaseControl. h // Animations /// Created by YouXianMing on 16/5/26. // Copyright©2016 YouXianMing. all rights reserved. // # import <UIKit/UIKit. h> @ class POPBaseControl; @ protocol POPBaseControlDelegate <NSObject>/*** scale percentage event ** @ param controll PressControll object * @ param percent percentage */-(void) POPBaseControl :( POPBaseControl *) controll currentPercent :( CGFloat) percent;/*** event trigger ** @ param controll PressControll object */-(void) POPBaseControlEvent :( POPBaseControl *) controll; @ end @ interface POPBaseControl: UIView/*** proxy */@ property (nonatomic, weak) id <POPBaseControlDelegate> delegate;/*** animation time, the default value is 0.4 */@ property (nonatomic) CFTimeInterval animationDuration;/*** target object */@ property (nonatomic, weak) id target; /*** event */@ property (nonatomic) SEL selector;/*** valid? */@ property (nonatomic) BOOL enabled; /*** select? */@ property (nonatomic) BOOL selected; # pragma mark-Properties used by SubClass & Methods Overwrite by subClass. /*** container view, used to add controls to sub-classes */@ property (nonatomic, strong, readonly) UIView * contentView;/*** current animation ratio (reload when subclass inherits) ** @ param percent ratio */-(void) currentPercent :( CGFloat) percent;/*** the event activates */-(void) controllEventActived; @ end
/// POPBaseControl. m // Animations /// Created by YouXianMing on 16/5/26. // Copyright©2016 YouXianMing. all rights reserved. // # import "POPBaseControl. h "# import" POP. h "@ interface POPBaseControl () @ property (nonatomic, strong) UIView * absView; @ property (nonatomic, strong) UIButton * button; @ property (nonatomic, strong) UIView * contentView; @ property (nonatomic) CGFloat percent; @ end @ implementation POPBaseControl-(void) layoutSubviews {[super layoutSubviews]; _ button. frame = CGRectMake (0, 0, self. frame. size. width, self. frame. size. height); _ contentView. bounds = CGRectMake (0, 0, self. frame. size. width, self. frame. size. height);}-(instancetype) initWithFrame :( CGRect) frame {if (self = [super initWithFrame: frame]) {// animation time _ animationDuration = 0.4f; // invisible view _ absView = [[UIView alloc] init]; _ absView. userInteractionEnabled = NO; _ absView. backgroundColor = [UIColor clearColor]; [self addSubview: _ absView]; // container View _ contentView = [[UIView alloc] initWithFrame: self. bounds]; _ contentView. userInteractionEnabled = NO; [self addSubview: _ contentView]; // button _ button = [[UIButton alloc] initWithFrame: self. bounds]; [self addSubview: _ button]; // button event [_ button addTarget: self action: @ selector (events) forControlEvents: UIControlEventTouchDown | UIControlEventTouchDragEnter]; [_ button addTarget: self action: @ selector (touchUpInside) forControlEvents: UIControlEventTouchUpInside]; [_ button addTarget: self action: @ selector (Counter) forControlEvents: UIControlEventTouchDragExit | counter];} return self ;} # pragma mark-Animations. -(void) touchUpInside {[self touchDragExitOrTouchCancel]; [self controllEventActived]; if (self.tar get & self. selector) {# pragma clang diagnostic push # pragma clang diagnostic ignored "-Warc-starter mselector-leaks" created self.tar get started mselector: self. selector withObject: self]; # pragma clang diagnostic pop} if (self. delegate & [self. delegate respondsToSelector: @ selector (POPBaseControlEvent :)]) {[self. delegate POPBaseControlEvent: self] ;}}-(void) touchDragExitOrTouchCancel {[_ absView. layer pop_removeAllAnimations]; POPBasicAnimation * scaleAnimation = [POPBasicAnimation animationWithPropertyNamed: kPOPLayerOpacity]; scaleAnimation. toValue = @ (1); scaleAnimation. delegate = self; scaleAnimation. duration = _ animationDuration; [_ absView. layer pop_addAnimation: scaleAnimation forKey: nil];}-(void) touchBeginOrTouchDragEnter {[_ absView. layer pop_removeAllAnimations]; POPBasicAnimation * scaleAnimation = [POPBasicAnimation animationWithPropertyNamed: kPOPLayerOpacity]; scaleAnimation. toValue = @ (0); scaleAnimation. delegate = self; scaleAnimation. duration = _ animationDuration; [_ absView. layer pop_addAnimation: scaleAnimation forKey: nil] ;}# pragma mark-POPAnimation's delegate. -(void) pop_animationDidApply :( POPAnimation *) anim {NSNumber * toValue = (NSNumber *) [anim valueForKeyPath: @ "currentValue"]; _ percent = (toValue. floatValue-[POPBaseControl calculateConstantWithX1: 0 y1: 1x2: 1 y2: 0])/[POPBaseControl calculateSlopeWithX1: 0 y1: 1x2: 1 y2: 0]; [self currentPercent: _ percent] ;}# pragma mark-Overwrite by subClass. -(void) currentPercent :( CGFloat) percent {}-(void) controllEventActived {}# pragma mark-Math. + (CGFloat) calculateSlopeWithX1 :( CGFloat) x1 y1 :( CGFloat) y1 x2 :( CGFloat) x2 y2 :( CGFloat) y2 {return (y2-y1)/(x2-x1 );} + (CGFloat) calculateConstantWithX1 :( CGFloat) x1 y1 :( CGFloat) y1 x2 :( CGFloat) x2 y2 :( CGFloat) y2 {return (y1 * (x2-x1) -x1 * (y2-y1)/(x2-x1);} # pragma mark-setter & getter. @ synthesize enabled = _ enabled;-(void) setEnabled :( BOOL) enabled {_ button. enabled = enabled;}-(BOOL) enabled {return _ button. enabled;} @ synthesize selected = _ selected;-(void) setSelected :( BOOL) selected {_ button. selected = selected;}-(BOOL) selected {return _ button. selected;} @ end
Description
I have encapsulated a base class control with three buttons. The above is an example to demonstrate how to achieve the desired effect through inheritance.