ios控件拖动改变位置的实现【转】
#define SCREEN_WIDTH ([UIScreen mainScreen].nativeBounds.size.width / [UIScreen mainScreen].nativeScale) #define SCREEN_HEIGHT ([UIScreen mainScreen].nativeBounds.size.height / [UIScreen mainScreen].nativeScale) #define SMALLWIDTH self.firstSmallPreView.frame.size.width //self.firstSmallPreView为响应拖动手势的控件 #define SMALLHEIGHT self.firstSmallPreView.frame.size.height #define animateDuration 0.3 //位置改变动画时间
//给拖动控件添加手势 UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(changeLocation:)]; [self.firstSmallPreView addGestureRecognizer:pan]; [self.view addSubview:self.firstSmallPreView];
第一种:任意拖动控件靠边停留实现:
- (void)changeLocation:(UIPanGestureRecognizer *)pan{ self.firstSmallPreView = pan.view; CGPoint panPoint = [pan locationInView:[[UIApplication sharedApplication] keyWindow]]; if(pan.state == UIGestureRecognizerStateBegan) { [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeStatus) object:nil]; } if(pan.state == UIGestureRecognizerStateChanged) { self.firstSmallPreView.center = CGPointMake(panPoint.x, panPoint.y); } else if(pan.state == UIGestureRecognizerStateEnded) { if(panPoint.x <= SCREEN_WIDTH/2) { if(panPoint.y <= 40 + SMALLHEIGHT/2 && panPoint.x >= 20 + SMALLWIDTH/2) { [UIView animateWithDuration:animateDuration animations:^{ self.firstSmallPreView.center = CGPointMake(panPoint.x, SMALLHEIGHT/2); }]; } else if(panPoint.y >= SCREEN_HEIGHT - SMALLHEIGHT/2 - 40 && panPoint.x >= 20 + SMALLWIDTH/2) { [UIView animateWithDuration:animateDuration animations:^{ self.firstSmallPreView.center = CGPointMake(panPoint.x, SCREEN_HEIGHT - SMALLHEIGHT/2); }]; } else if (panPoint.x < SMALLWIDTH/2+20 && panPoint.y > SCREEN_HEIGHT-SMALLHEIGHT/2) { [UIView animateWithDuration:animateDuration animations:^{ self.firstSmallPreView.center = CGPointMake(SMALLWIDTH/2, SCREEN_HEIGHT-SMALLHEIGHT/2); }]; } else { CGFloat pointy = panPoint.y < SMALLHEIGHT/2 ? SMALLHEIGHT/2 :panPoint.y; [UIView animateWithDuration:animateDuration animations:^{ self.firstSmallPreView.center = CGPointMake(SMALLWIDTH/2, pointy); }]; } } else if(panPoint.x > SCREEN_WIDTH/2) { if(panPoint.y <= 40 + SMALLHEIGHT/2 && panPoint.x < SCREEN_WIDTH - SMALLWIDTH/2-20 ) { [UIView animateWithDuration:animateDuration animations:^{ self.firstSmallPreView.center = CGPointMake(panPoint.x, SMALLHEIGHT/2); }]; } else if(panPoint.y >= SCREEN_HEIGHT - 40 - SMALLHEIGHT/2 && panPoint.x < SCREEN_WIDTH - SMALLWIDTH/2 - 20) { [UIView animateWithDuration:animateDuration animations:^{ self.firstSmallPreView.center = CGPointMake(panPoint.x, SCREEN_HEIGHT - SMALLHEIGHT/2); }]; } else if (panPoint.x > SCREEN_WIDTH- SMALLWIDTH/2-20 && panPoint.y < SMALLHEIGHT/2) { [UIView animateWithDuration:animateDuration animations:^{ self.firstSmallPreView.center = CGPointMake(SCREEN_WIDTH - SMALLWIDTH/2, SMALLHEIGHT/2); }]; } else { CGFloat pointy = panPoint.y > SCREEN_HEIGHT - SMALLHEIGHT/2 ? SCREEN_HEIGHT- SMALLHEIGHT/2 :panPoint.y; [UIView animateWithDuration:animateDuration animations:^{ self.firstSmallPreView.center = CGPointMake(SCREEN_WIDTH - SMALLWIDTH/2, pointy); }]; } } } } - (void)changeStatus { [UIView animateWithDuration:0.5 animations:^{ CGFloat x = self.firstSmallPreView.center.x < 20 + SMALLWIDTH/2 ? 0 : self.firstSmallPreView.center.x > SCREEN_WIDTH - 20 - SMALLWIDTH/2 ? SCREEN_WIDTH : self.firstSmallPreView.center.x; CGFloat y = self.firstSmallPreView.center.y < 40 + SMALLHEIGHT/2 ? 0 : self.firstSmallPreView.center.y > SCREEN_HEIGHT - 40 - SMALLHEIGHT/2 ? SCREEN_HEIGHT : self.firstSmallPreView.center.y; //禁止停留在4个角 if((x == 0 && y ==0) || (x == SCREEN_WIDTH && y == 0) || (x == 0 && y == SCREEN_HEIGHT) || (x == SCREEN_WIDTH && y == SCREEN_HEIGHT)){ y = self.firstSmallPreView.center.y; } self.firstSmallPreView.center = CGPointMake(x, y); }]; }
第二种:任意拖动,任意停留
- (void)changeLocation:(UIPanGestureRecognizer *)pan{ self.firstSmallPreView = pan.view; CGPoint panPoint = [pan locationInView:[[UIApplication sharedApplication] keyWindow]]; if(pan.state == UIGestureRecognizerStateBegan) { [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(changeStatus) object:nil]; } if(pan.state == UIGestureRecognizerStateChanged) { self.firstSmallPreView.center = CGPointMake(panPoint.x, panPoint.y); } else if(pan.state == UIGestureRecognizerStateEnded) { CGFloat pointy = panPoint.y; CGFloat pointx = panPoint.x; BOOL needChangeLocation = NO; if (panPoint.y > SCREEN_HEIGHT - SMALLHEIGHT /2) { pointy = SCREEN_HEIGHT - SMALLHEIGHT /2; needChangeLocation = YES; } if (panPoint.y < SMALLHEIGHT/2) { pointy = SMALLHEIGHT/2; needChangeLocation = YES; } if (panPoint.x > SCREEN_WIDTH - SMALLWIDTH/2) { pointx = SCREEN_WIDTH - SMALLWIDTH/2; needChangeLocation = YES; } if (panPoint.x < SMALLWIDTH/2) { pointx = SMALLWIDTH/2; needChangeLocation = YES; } if (needChangeLocation) { [UIView animateWithDuration:animateDuration animations:^{ self.firstSmallPreView.center = CGPointMake(pointx, pointy); }]; } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!