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

iOS實(shí)現(xiàn)懸浮按鈕

 更新時(shí)間:2021年01月25日 08:01:09   作者:林大帥6688  
這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)懸浮按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前言

開發(fā)中會(huì)遇到有懸浮按鈕功能。

效果

上代碼

SuspensionButton.h

#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interface SuspensionButton : UIButton

@property(nonatomic, assign)BOOL MoveEnable;
@property(nonatomic, assign)BOOL MoveEnabled;
@property(nonatomic, assign)CGPoint beginpoint;

@end

NS_ASSUME_NONNULL_END

SuspensionButton.m

#import "SuspensionButton.h"

@implementation SuspensionButton

- (instancetype)initWithFrame:(CGRect)frame {
 if (self = [super initWithFrame:frame]) {
 self.frame = CGRectMake([[UIScreen mainScreen] bounds].size.width - 51, 50, 51, 51);
 [self setBackgroundImage:[UIImage imageNamed:@"icon_move"] forState:UIControlStateNormal];
 [self setTitle:@"Button" forState:UIControlStateNormal];
 self.titleLabel.font = [UIFont systemFontOfSize:10];
 [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
 _MoveEnable = YES;
 }
 return self;
}

//開始觸摸的方法
//觸摸-清掃
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 _MoveEnabled = NO;
 [super touchesBegan:touches withEvent:event];
 if (!_MoveEnable) {
 return;
 }
 UITouch *touch = [touches anyObject];
 _beginpoint = [touch locationInView:self];
}

//觸摸移動(dòng)的方法
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
 _MoveEnabled = YES;//單擊事件可用
 if (!_MoveEnable) {
 return;
 }
 UITouch *touch = [touches anyObject];
 CGPoint currentPosition = [touch locationInView:self];
 //偏移量
 float offsetX = currentPosition.x - _beginpoint.x;
 float offsetY = currentPosition.y - _beginpoint.y;
 //移動(dòng)后的中心坐標(biāo)
 self.center = CGPointMake(self.center.x + offsetX, self.center.y + offsetY);
 
 //x軸左右極限坐標(biāo)
 if (self.center.x > (self.superview.frame.size.width - self.frame.size.width / 2)) {
 CGFloat x = self.superview.frame.size.width - self.frame.size.width / 2;
 self.center = CGPointMake(x, self.center.y + offsetY);
 } else if (self.center.x < self.frame.size.width / 2) {
 CGFloat x = self.frame.size.width / 2;
 self.center = CGPointMake(x, self.center.y + offsetY);
 }
 
 //y軸上下極限坐標(biāo)
 if (self.center.y > (self.superview.frame.size.height - self.frame.size.height)) {
 CGFloat x = self.center.x;
 CGFloat y = self.superview.frame.size.height - self.frame.size.height * 1.5;
 self.center = CGPointMake(x, y);
 } else if (self.center.y <= self.frame.size.height) {
 CGFloat x = self.center.x;
 CGFloat y = self.frame.size.height * 1.2;
 self.center = CGPointMake(x, y);
 }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
 if (!_MoveEnable) {
 return;
 }
 
 if (self.center.x >= self.superview.frame.size.width / 2) {//向右側(cè)移動(dòng)
 //偏移動(dòng)畫
 [UIView beginAnimations:@"move" context:nil];
 [UIView setAnimationDuration:1];
 [UIView setAnimationDelegate:self];
 self.frame = CGRectMake(self.superview.frame.size.width - 51, self.center.y - 25.5, 51, 51);
 //提交UIView動(dòng)畫
 [UIView commitAnimations];
 } else {//向左側(cè)移動(dòng)
 
 [UIView beginAnimations:@"move" context:nil];
 [UIView setAnimationDuration:1];
 [UIView setAnimationDelegate:self];
 self.frame=CGRectMake(0.f,self.center.y - 25.5, 51, 51);
 //提交UIView動(dòng)畫
 [UIView commitAnimations];
 }
 
 //不加此句話,UIButton將一直處于按下狀態(tài)
 [super touchesEnded: touches withEvent: event];
 
}
@end

使用

ViewController.m

#import "ViewController.h"
#import "SuspensionButton.h"http://懸浮按鈕

@interface ViewController ()

@property(nonatomic, strong) SuspensionButton *suspensionButton;

@end

@implementation ViewController

- (void)viewDidLoad {
 [super viewDidLoad];
 [self.view addSubview:self.suspensionButton];
}

- (SuspensionButton *)suspensionButton {
 if(_suspensionButton == nil) {
 _suspensionButton = [SuspensionButton buttonWithType:UIButtonTypeCustom];
 _suspensionButton.backgroundColor = [UIColor grayColor];
 _suspensionButton.layer.masksToBounds = YES;
 _suspensionButton.layer.cornerRadius = self.suspensionButton.frame.size.width/2;
 [_suspensionButton addTarget:self action:@selector(suspensionButtonClick) forControlEvents:UIControlEventTouchUpInside];
 }
 return _suspensionButton;
}

- (void)suspensionButtonClick {
 
}

@end

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IOS UITableView顏色設(shè)置的實(shí)例詳解

    IOS UITableView顏色設(shè)置的實(shí)例詳解

    這篇文章主要介紹了IOS UITableView顏色設(shè)置的實(shí)例詳解的相關(guān)資料,這里提供了幾種方法幫助大家掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-08-08
  • IOS9.0 LaunchScreen.StroyBoard自定義啟動(dòng)圖片詳解

    IOS9.0 LaunchScreen.StroyBoard自定義啟動(dòng)圖片詳解

    這篇文章主要介紹了IOS9.0 LaunchScreen.StroyBoard自定義啟動(dòng)圖片詳解的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • iOS開發(fā)KVO實(shí)現(xiàn)細(xì)節(jié)解密

    iOS開發(fā)KVO實(shí)現(xiàn)細(xì)節(jié)解密

    這篇文章主要為大家介紹了iOS開發(fā)KVO實(shí)現(xiàn)細(xì)節(jié)解密,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • iOS 11 BUG的發(fā)現(xiàn)、定位和解決

    iOS 11 BUG的發(fā)現(xiàn)、定位和解決

    這篇文章主要為大家詳細(xì)介紹了iOS 11 BUG的發(fā)現(xiàn)、定位和解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • iOS 泛型中nullable、null resettable、null kindof 用法詳解

    iOS 泛型中nullable、null resettable、null kindof 用法詳解

    這篇文章主要介紹了iOS 泛型中nullable、null resettable、null kindof 用法詳解的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-09-09
  • iOS實(shí)現(xiàn)折疊單元格

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

    這篇文章主要為大家詳細(xì)介紹了iOS實(shí)現(xiàn)折疊單元格,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-09-09
  • Nib文件是什么?Nib文件打開方法

    Nib文件是什么?Nib文件打開方法

    這篇文章主要介紹了Nib文件是什么?Nib文件打開方法,nib文件是Cocoa App的界面資源,這種文件直接用xcode是不能被打開的,要修改后才能打開,本文就講解了修改方法,需要的朋友可以參考下
    2015-04-04
  • iOS TableView頭視圖根據(jù)偏移量下拉縮放效果

    iOS TableView頭視圖根據(jù)偏移量下拉縮放效果

    這篇文章主要為大家詳細(xì)介紹了iOS TableView頭視圖根據(jù)偏移量下拉縮放效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 總結(jié)IOS關(guān)閉鍵盤/退出鍵盤的五種方式

    總結(jié)IOS關(guān)閉鍵盤/退出鍵盤的五種方式

    IOS開發(fā)中經(jīng)常要用到輸入框,默認(rèn)情況下點(diǎn)擊輸入框就會(huì)彈出鍵盤,但是必須要實(shí)現(xiàn)輸入框return的委托方法才能取消鍵盤的顯示,對(duì)于用戶體驗(yàn)來(lái)說(shuō)很不友好,我們可以實(shí)現(xiàn)例如點(diǎn)擊鍵盤以外的空白區(qū)域來(lái)將鍵盤關(guān)閉的功能,以下是我總結(jié)出的幾種關(guān)閉鍵盤的方法。
    2016-08-08
  • iOS開發(fā)輸入自動(dòng)填充UITextField背景色

    iOS開發(fā)輸入自動(dòng)填充UITextField背景色

    如何在iOS中實(shí)現(xiàn)輸入時(shí)自動(dòng)填充背景色的效果,首先,我們?cè)O(shè)置UITextField的背景色為初始顏色,然后,通過(guò)設(shè)置UITextField的代理,并監(jiān)聽UITextField的輸入事件,我們?cè)谟脩糸_始輸入時(shí)將其背景色改變?yōu)楦吡令伾?在用戶結(jié)束輸入時(shí)恢復(fù)為初始顏色
    2023-10-10

最新評(píng)論

新巴尔虎左旗| 龙南县| 大邑县| 平湖市| 资中县| 旬邑县| 民勤县| 南城县| 泰来县| 岫岩| 西昌市| 阳山县| 宣武区| 辽中县| 紫金县| 江城| 西林县| 永仁县| 开阳县| 赞皇县| 来宾市| 张家口市| 鄯善县| 南溪县| 汉沽区| 阜宁县| 沾化县| 黄平县| 瑞安市| 平江县| 慈利县| 绥滨县| 内丘县| 河北省| 红安县| 呼玛县| 威海市| 建瓯市| 新乡市| 马关县| 怀化市|