控件任意拖动 iOS

2019-12-04  本文已影响0人  哇哇卡

以下是拖动的两种需求:

需求一、任意拖动控件靠边停留实现

需求二、任意拖动,任意停留(不要求必须靠边停留)

#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);

}];

}

}

}

上一篇下一篇

猜你喜欢

热点阅读