溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

iOS如何實現(xiàn)一個可以在屏幕中自由移動的按鈕

發(fā)布時間:2021-06-30 13:36:43 來源:億速云 閱讀:311 作者:小新 欄目:移動開發(fā)

這篇文章主要為大家展示了“iOS如何實現(xiàn)一個可以在屏幕中自由移動的按鈕”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“iOS如何實現(xiàn)一個可以在屏幕中自由移動的按鈕”這篇文章吧。

效果圖如下:

iOS如何實現(xiàn)一個可以在屏幕中自由移動的按鈕

其實實現(xiàn)很簡單,只需要寫.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&&centerPoint.x>leftMargin) {
     _crossBtn.center = CGPointMake(beginPoint.x + offsetX, bottomMargin);
    }
   }
   else if (centerPoint.y<topMargin)
   {
    if (centerPoint.x<rightMargin&&centerPoint.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如何實現(xiàn)一個可以在屏幕中自由移動的按鈕”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

ios
AI