最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS自定義button抖動效果并實(shí)現(xiàn)右上角刪除按鈕

 更新時(shí)間:2016年03月22日 11:12:17   作者:明月釣無痕  
這篇文章主要為大家詳細(xì)介紹了iOS自定義button抖動效果并實(shí)現(xiàn)右上角刪除按鈕的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

遇到過這種需求要做成類似與蘋果刪除軟件時(shí)的動態(tài)效果。

1.長按抖動;
2.抖動時(shí)出現(xiàn)一個(gè)X;
3.點(diǎn)擊x,刪除button;
4.抖動時(shí),點(diǎn)擊按鈕,停止抖動;

下面是我的設(shè)計(jì)思路:

1.繼承UIButton;
2.給button在右上角添加一個(gè)按鈕;
3.給button添加長按手勢;
4.給button添加遮蓋,抖動時(shí)可以攔截點(diǎn)擊事件;

有更好的做法,還請斧正。

// .m文件

#import "DZDeleteButton.h"
#import "UIView+Extension.h" // 這個(gè)只是為了方便取寬高的一個(gè)分類,代碼就不貼了

@interface DZDeleteButton ()

// 是否抖動
@property (nonatomic, assign, getter=isShaking) BOOL shaking;
// 右上角的按鈕,
@property (nonatomic, weak) UIImageView *iconBtn;
// 遮蓋,在抖動時(shí)出現(xiàn)
@property (nonatomic, weak) UIView *coverView;
@end

@implementation DZDeleteButton

- (UIImageView *)iconBtn {
 if (!_iconBtn) {
  UIImageView *iconBtn = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"delete"]];
  iconBtn.userInteractionEnabled = YES;
  iconBtn.hidden = YES;
  _iconBtn = iconBtn;
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconClick)];
  [iconBtn addGestureRecognizer:tap];
  [self addSubview:iconBtn];
 }
 return _iconBtn;
}

- (UIView *)coverView {
 if (!_coverView) {
  UIView *view = [[UIView alloc] init];
  view.backgroundColor = [UIColor clearColor];
  view.hidden = YES;
  UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(coverClick)];
  [view addGestureRecognizer:tap];
  [self addSubview:view];
  _coverView = view;
 }
 return _coverView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
 self = [super initWithFrame:frame];
 if (self) {
  [self addLongPressGestureRecognizer];
 }
 return self;
}

- (instancetype)init
{
 self = [super init];
 if (self) {
  [self addLongPressGestureRecognizer];
 }
 return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
 self = [super initWithCoder:coder];
 if (self) {
  [self addLongPressGestureRecognizer];
 }
 return self;
}

// 添加長按手勢
- (void)addLongPressGestureRecognizer {
 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longClick)];
 [self addGestureRecognizer:longPress];
}

- (void)delete {
 [self.iconBtn.superview removeFromSuperview];
}

// 是否執(zhí)行動畫
- (void)setShaking:(BOOL)shaking {
 if (shaking) {
  [self shakingAnimation];
  self.coverView.hidden = NO;
  self.iconBtn.hidden = NO;
 } else {
  [self.layer removeAllAnimations];
  self.coverView.hidden = YES;
  self.iconBtn.hidden = YES;
 }
}

#pragma mark - 抖動動畫
#define Angle2Radian(angle) ((angle) / 180.0 * M_PI)
- (void)shakingAnimation {
 CAKeyframeAnimation *anim = [CAKeyframeAnimation animation];
 anim.keyPath = @"transform.rotation";

 anim.values = @[@(Angle2Radian(-5)), @(Angle2Radian(5)), @(Angle2Radian(-5))];
 anim.duration = 0.25;

 // 動畫次數(shù)設(shè)置為最大
 anim.repeatCount = MAXFLOAT;
 // 保持動畫執(zhí)行完畢后的狀態(tài)
 anim.removedOnCompletion = NO;
 anim.fillMode = kCAFillModeForwards;

 [self.layer addAnimation:anim forKey:@"shake"];
}

- (void)longClick {
 if (self.shaking) return;
 self.shaking = YES;
}

// 點(diǎn)擊右上角按鈕
- (void)iconClick {
 [self removeFromSuperview];
// 設(shè)計(jì)一個(gè)代理,為了在自己被刪除后做一些事情(例如,對頁面進(jìn)行布局)
 if ([self.delegate respondsToSelector:@selector(deleteButtonRemoveSelf:)]) {
  [self.delegate deleteButtonRemoveSelf:self];
 }
}

- (void)coverClick {
 self.shaking = NO;
}

- (void)layoutSubviews {
 [super layoutSubviews];

 // 調(diào)整位置
 self.imageView.x = 0;
 self.imageView.y = 0;
 self.imageView.width = self.width;
 self.imageView.height = self.width;

 self.titleLabel.x = 0;
 self.titleLabel.width = self.width;
 if (self.width >= self.height) {
  self.titleLabel.height = 20;
  self.titleLabel.y = self.height - self.titleLabel.height;
 } else {
  self.titleLabel.y = self.imageView.height;
  self.titleLabel.height = self.height - self.titleLabel.y;
 }

 self.titleLabel.textAlignment = NSTextAlignmentCenter;
 self.iconBtn.size = CGSizeMake(self.width * 0.3, self.width * 0.3);
 self.iconBtn.x = self.width - self.iconBtn.width;
 self.iconBtn.y = 0;

 self.coverView.frame = self.bounds;
 [self bringSubviewToFront:self.iconBtn];
}

@end
// .h文件 只有一個(gè)代理
#import <UIKit/UIKit.h>

@class DZDeleteButton;
@protocol DZDeleteButtonDelegate <NSObject>
@optional
- (void)deleteButtonRemoveSelf:(DZDeleteButton *)button;
@end

@interface DZDeleteButton : UIButton
@property (nonatomic, weak) id<DZDeleteButtonDelegate> delegate;

@end

上面效果圖在vc中的代碼

- (void)viewDidLoad {
 [super viewDidLoad];

 DZDeleteButton *button = [[DZDeleteButton alloc] init];
 [button setImage:[UIImage imageNamed:@"bj"] forState:UIControlStateNormal];
 [button setTitle:@"百思" forState:UIControlStateNormal];
 button.delegate = self;
 button.frame = CGRectMake(20, 20, 60, 80);
 [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
 [button addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
 [self.view addSubview:button];
}

- (void)btnClick {
 NSLog(@"點(diǎn)擊button");
}

- (void)deleteButtonRemoveSelf:(DZDeleteButton *)button {
 NSLog(@"已經(jīng)刪除,要做什么事");
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • 如何去掉Xcode工程中某種類型的警告

    如何去掉Xcode工程中某種類型的警告

    這篇文章主要給大家介紹了關(guān)于如何去掉Xcode工程中某種類型的警告,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Xcode具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • iOS開發(fā)之路--微博“更多”頁面

    iOS開發(fā)之路--微博“更多”頁面

    本文是IOS開發(fā)之路系列文章第五篇,主要講訴了,如何制作微博的更多頁面,并附上效果圖及源碼,需要的朋友可以參考下,希望能有所幫助
    2014-08-08
  • iOS11帶來的技術(shù)變化及注意事項(xiàng)

    iOS11帶來的技術(shù)變化及注意事項(xiàng)

    這篇文章給大家介紹了ios11帶來了技術(shù)變化及注意事項(xiàng),在相冊權(quán)限上有很大變化,具體內(nèi)容詳情大家參考下本文
    2017-09-09
  • iOS實(shí)現(xiàn)折疊單元格

    iOS實(shí)現(xiàn)折疊單元格

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)折疊單元格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • iOS設(shè)置UIButton文字顯示位置和字體大小、顏色的方法

    iOS設(shè)置UIButton文字顯示位置和字體大小、顏色的方法

    這篇文章給大家分享了iOS如何設(shè)置UIButton的文字顯示位置和字體的大小、顏色,文中給出了示例代碼,相信對大家的學(xué)習(xí)和理解很有幫助,有需要的朋友們下面來一起看看吧。
    2016-09-09
  • iOS中Block的回調(diào)使用和解析詳解

    iOS中Block的回調(diào)使用和解析詳解

    剛剛進(jìn)入iOS開發(fā)行業(yè),發(fā)現(xiàn)開發(fā)中要用到大量的block回調(diào),由此可見它的重要性。本文主要講的是 Block 回調(diào)的使用,以及 Block 是如何實(shí)現(xiàn)這種神奇的回調(diào)兩部分來講的,下面來一起看看吧。
    2016-09-09
  • Xcode中代碼注釋編寫的一些小技巧

    Xcode中代碼注釋編寫的一些小技巧

    如何在 Xcode 中編寫規(guī)范注釋,規(guī)范注釋可以在Xcode的快速幫助檢查器(quickheliector)中顯示,這篇文章主要給大家介紹了關(guān)于Xcode中代碼注釋編寫的一些小技巧,需要的朋友可以參考下
    2021-10-10
  • iOS tableView多輸入框如何獲取數(shù)據(jù)

    iOS tableView多輸入框如何獲取數(shù)據(jù)

    這篇文章主要給大家介紹了關(guān)于iOS tableView多輸入框如何獲取數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • iOS實(shí)現(xiàn)類似格瓦拉電影的轉(zhuǎn)場動畫

    iOS實(shí)現(xiàn)類似格瓦拉電影的轉(zhuǎn)場動畫

    這篇文章主要給大家介紹了利用iOS如何實(shí)現(xiàn)類似格瓦拉電影的轉(zhuǎn)場動畫,文中給出了詳細(xì)步驟實(shí)現(xiàn)代碼,對大家的學(xué)習(xí)和理解很有幫助,有需要的朋友們可以參考借鑒,下面來一起看看吧。
    2016-11-11
  • iOS 報(bào)clang: error: no input files錯(cuò)誤的解決方法

    iOS 報(bào)clang: error: no input files錯(cuò)誤的解決方法

    這篇文章主要給大家介紹了關(guān)于iOS報(bào)clang: error: no input files錯(cuò)誤的解決方法,文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01

最新評論

八宿县| 观塘区| 洛宁县| 奈曼旗| 泰州市| 平利县| 台山市| 邓州市| 镇沅| 岗巴县| 阳信县| 龙门县| 林西县| 康乐县| 许昌市| 祥云县| 邳州市| 镇巴县| 孙吴县| 阳春市| 焦作市| 南木林县| 莱阳市| 台东市| 德化县| 红原县| 石林| 瓦房店市| 黎城县| 柘城县| 溆浦县| 盐亭县| 南岸区| 金沙县| 宾川县| 新余市| 临颍县| SHOW| 和平区| 万全县| 新宾|