BaseControl按鈕合集

来源:http://www.cnblogs.com/YouXianMing/archive/2016/05/26/5532116.html
-Advertisement-
Play Games

BaseControl按鈕合集 效果 源碼 https://github.com/YouXianMing/Animations 說明 本人一共封裝了3種按鈕的基類控制項,以上是一個示例演示,演示如何通過繼承來實現想要的效果. ...


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種按鈕的基類控制項,以上是一個示例演示,演示如何通過繼承來實現想要的效果.

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 環境: Sql Server2012 SP3企業版,Windows Server2008 標準版 問題由來: 最近在做DB優化的時候,發現一個存儲過程有非常嚴重的性能問題, 由於整個SP整體邏輯是一個多表關聯的複雜的查詢,整體結構比較複雜的,通過的分析和嘗試, 最後發現問題出在其中一個大表的查詢上實 ...
  • 獲取SQL執行計劃的常見幾種方法 一、獲取庫緩衝區中的執行計劃 1. 查詢v$sql動態性能視圖,找到要查詢的SQL語句的sql_id。 2. 調用dbms_xplan包的display_cursor方法,查看該語句執行時的執行計劃。 例如: SELECT dname FROM emp, dept ...
  • 利用PL/SQL可以進行模塊化程式設計。 在一個PL/SQL塊中,可以定義若幹個子程式。 把一些功能相對獨立、需要經常執行的代碼定義為一個子程式,在需要時根據子程式的名字進行調用。這樣不僅便於程式設計和編碼,而且利於程式的調試。PL/SQL有兩種形式的子程式,即過程和函數。 在子程式中也可以定義變數 ...
  • 談完並行執行的原理,咱們再來談談優化,到底並行執行能給我們帶來哪些好處,我們又應該註意什麼呢,下麵展開. Amdahl’s Law 再談並行優化前我想有必要談談阿姆達爾定律,可惜老爺子去年已經駕鶴先去了. 其中P:可以並行的百分比 N:演算法並行計算使用的”CPU” 這裡我們舉個簡單的例子,我們來做一 ...
  • 編寫PL/SQL塊的主要目的是對資料庫進行訪問,因此,在PL/SQL塊中可以包含SELECT語句、DML語句,還可以包含DCL語句。需要註意的是,在PL/SQL塊中不能直接包含DDL語句,如果要利用PL/SQL塊完成諸如創建表、修改表結構等操作,需要通過其他方法。通過SQL語句以及流控制語句,可以編 ...
  • 1.下載 mysql-mysql-5.1.55-win32.zip 2. 解壓縮到任何一個目錄,最好目錄名稱不要有空格; 例如:C:\mysql 3. 刪除Embedded,include,lib,mysql-test 這四個目錄。 註:這四個目錄占了300多M,刪除後,只有不到100M。 3,在c... ...
  • 1 NSURLConnettion NSURLConnettion是 Core Foundation/CFNetwork框架 API 之上的一個抽象. NSURLConnettion是用來指代 Foundation 框架中的一系列組件: NSURLRequest,NSURLResponse,NSUR ...
  • 1.效果圖 2.源代碼 MainActivity.java public class MainActivity extends AppCompatActivity { public enum ChartType { LINE_CHART, COLUMN_CHART, PIE_CHART, BUBBL... ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...