這篇文章主要為大家展示了“iOS如何實現一個可以在屏幕中自由移動的按鈕”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“iOS如何實現一個可以在屏幕中自由移動的按鈕”這篇文章吧。
創新互聯公司電話聯系:13518219792,為您提供成都網站建設網頁設計及定制高端網站建設服務,創新互聯公司網頁制作領域10余年,包括成都圍欄護欄等多個方面擁有豐富的網站營銷經驗,選擇創新互聯公司,為企業錦上添花!效果圖如下:
其實實現很簡單,只需要寫.m就可以了
示例代碼
#import "CrossBtnVC.h"
@interface CrossBtnVC () { CGPoint beginPoint; CGFloat rightMargin; CGFloat leftMargin; CGFloat topMargin; CGFloat bottomMargin; CGMutablePathRef pathRef; } @property (nonatomic,strong) UIButton *crossBtn;//聊天移動 @end
@implementation CrossBtnVC
- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; _crossBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_crossBtn setImage:[UIImage imageNamed:@"移動聊天"] forState:UIControlStateNormal]; _crossBtn.frame = CGRectMake(UI_View_Width-54*UI_Width_Scale, UI_View_Height-103, 40, 40); [self.view addSubview:_crossBtn]; [_crossBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside]; UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)]; [_crossBtn addGestureRecognizer:pan]; rightMargin = [UIScreen mainScreen].bounds.size.width-30; leftMargin = 30; bottomMargin = [UIScreen mainScreen].bounds.size.height-30-50; topMargin = 30+64; pathRef=CGPathCreateMutable(); CGPathMoveToPoint(pathRef, NULL, leftMargin, topMargin); CGPathAddLineToPoint(pathRef, NULL, rightMargin, topMargin); CGPathAddLineToPoint(pathRef, NULL, rightMargin, bottomMargin); CGPathAddLineToPoint(pathRef, NULL, leftMargin, bottomMargin); CGPathAddLineToPoint(pathRef, NULL, leftMargin, topMargin); CGPathCloseSubpath(pathRef); }
#pragma mark - 事件 - (void)btnAction:(UIButton*)sender{ }
#pragma mark - 手勢 - (void)handlePan:(UIPanGestureRecognizer *)pan { if (pan.state == UIGestureRecognizerStateBegan) { beginPoint = [pan locationInView:self.view]; }else if (pan.state == UIGestureRecognizerStateChanged){ CGPoint nowPoint = [pan locationInView:self.view]; float offsetX = nowPoint.x - beginPoint.x; float offsetY = nowPoint.y - beginPoint.y; CGPoint centerPoint = CGPointMake(beginPoint.x + offsetX, beginPoint.y + offsetY); if (CGPathContainsPoint(pathRef, NULL, centerPoint, NO)) { _crossBtn.center = centerPoint; }else{ if (centerPoint.y>bottomMargin) { if (centerPoint.x<rightMargin&¢erPoint.x>leftMargin) { _crossBtn.center = CGPointMake(beginPoint.x + offsetX, bottomMargin); } } else if (centerPoint.y<topMargin) { if (centerPoint.x<rightMargin&¢erPoint.x>leftMargin) { _crossBtn.center = CGPointMake(beginPoint.x + offsetX, topMargin); } } else if (centerPoint.x>rightMargin) { _crossBtn.center = CGPointMake(rightMargin, beginPoint.y + offsetY); } else if (centerPoint.x<leftMargin) { _crossBtn.center = CGPointMake(leftMargin, beginPoint.y + offsetY); } } }else if (pan.state == UIGestureRecognizerStateEnded || pan.state == UIGestureRecognizerStateFailed){ } } @end
以上是“iOS如何實現一個可以在屏幕中自由移動的按鈕”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注創新互聯網站建設公司行業資訊頻道!
另外有需要云服務器可以了解下創新互聯建站vcdvsql.cn,海內外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業上云的綜合解決方案,具有“安全穩定、簡單易用、服務可用性高、性價比高”等特點與優勢,專為企業上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享題目:iOS如何實現一個可以在屏幕中自由移動的按鈕-創新互聯
本文鏈接:http://vcdvsql.cn/article14/egdde.html
成都網站建設公司_創新互聯,為您提供建站公司、移動網站建設、虛擬主機、營銷型網站建設、網站改版、定制開發
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯