BaseControl按鈕合集,basecontrol合集

來源:互聯網
上載者:User

BaseControl按鈕合集,basecontrol合集

BaseControl按鈕合集

 

效果

 

源碼

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>/** *  縮放百分比事件 * *  @param controll PressControll對象 *  @param percent  百分比 */- (void)POPBaseControl:(POPBaseControl *)controll currentPercent:(CGFloat)percent;/** *  事件觸發 * *  @param controll PressControll對象 */- (void)POPBaseControlEvent:(POPBaseControl *)controll;@end@interface POPBaseControl : UIView/** *  代理 */@property (nonatomic, weak) id <POPBaseControlDelegate>  delegate;/** *  動畫時間,預設值為0.4 */@property (nonatomic) CFTimeInterval animationDuration;/** *  目標對象 */@property (nonatomic, weak) id target;/** *  事件 */@property (nonatomic) SEL      selector;/** *  是否有效 */@property (nonatomic) BOOL     enabled;/** *  是否選中 */@property (nonatomic) BOOL     selected;#pragma mark - Properties used by SubClass & Methods Overwrite by subClass./** *  容器view,用於子類添加控制項 */@property (nonatomic, strong, readonly) UIView  *contentView;/** *  當前動畫比例(子類繼承的時候重載) * *  @param percent 比例 */- (void)currentPercent:(CGFloat)percent;/** *  事件啟用了 */- (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]) {                // 動畫時間        _animationDuration = 0.4f;                // 隱藏的view        _absView                        = [[UIView alloc] init];        _absView.userInteractionEnabled = NO;        _absView.backgroundColor        = [UIColor clearColor];        [self addSubview:_absView];                // 容器View        _contentView                        = [[UIView alloc] initWithFrame:self.bounds];        _contentView.userInteractionEnabled = NO;        [self addSubview:_contentView];                // 按鈕        _button = [[UIButton alloc] initWithFrame:self.bounds];        [self addSubview:_button];                // 按鈕事件        [_button addTarget:self action:@selector(touchBeginOrTouchDragEnter) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragEnter];        [_button addTarget:self action:@selector(touchUpInside)   forControlEvents:UIControlEventTouchUpInside];        [_button addTarget:self action:@selector(touchDragExitOrTouchCancel)   forControlEvents:UIControlEventTouchDragExit | UIControlEventTouchCancel];    }        return self;}#pragma mark - Animations.- (void)touchUpInside {        [self touchDragExitOrTouchCancel];        [self controllEventActived];        if (self.target && self.selector) {        #pragma clang diagnostic push#pragma clang diagnostic ignored "-Warc-performSelector-leaks"        [self.target performSelector: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:1 x2:1 y2:0]) / [POPBaseControl calculateSlopeWithX1:0 y1:1 x2: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

 

說明

本人一共封裝了3種按鈕的基類控制項,以上是一個樣本示範,示範如何通過繼承來實現想要的效果.

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.