UIPanGestureRecognizer *thisPan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(thisViewPanClick:)];
[self addGestureRecognizer:thisPan]; //拖动
UILongPressGestureRecognizer *longPan = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(thisViewLongPanClick:)];
[self addGestureRecognizer:longPan];//长按
- (void)thisViewLongPanClick:(UILongPressGestureRecognizer *)pan {
CGPoint currentPoint = [pan locationInView:self];
CGPoint trPoint = CGPointMake(currentPoint.x - touchOffset.x, currentPoint.y - touchOffset.y);
touchOffset = currentPoint;
switch (pan.state) {
case UIGestureRecognizerStateBegan:
{
[self getCurSelectBut:currentPoint];
self.hidden = YES;
[[ASMagnifierManger sharedInstance] magnifierPoint_Began:CGPointMake(self.selectBut.width/2, self.selectBut.height/2) withView:self.selectBut];
//振动
AudioServicesPlaySystemSound(1519);
self.hidden = NO;
break;
}
case UIGestureRecognizerStateChanged:
{
[self moveTouchPoint:trPoint];
[[ASMagnifierManger sharedInstance] magnifierPoint_Moved:CGPointMake(self.selectBut.width/2, self.selectBut.height/2) withView:self.selectBut];
break;
}
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateFailed:
{
self.selectBut = nil;
[[ASMagnifierManger sharedInstance] magnifier_Ended];
if (self.modifyCallBlock) {
self.modifyCallBlock(YES);
}
break;
}
default:
break;
}
}
- (void)thisViewPanClick:(UIPanGestureRecognizer *)pan {
CGPoint currentPoint = [pan locationInView:self];
CGPoint trPoint = [pan translationInView:self];
switch (pan.state) {
case UIGestureRecognizerStateBegan:
{
[self getCurSelectBut:currentPoint];
self.hidden = YES;
[[ASMagnifierManger sharedInstance] magnifierPoint_Began:CGPointMake(self.selectBut.width/2, self.selectBut.height/2) withView:self.selectBut];
//振动
AudioServicesPlaySystemSound(1519);
self.hidden = NO;
break;
}
case UIGestureRecognizerStateChanged:
{
[self moveTouchPoint:trPoint];
[[ASMagnifierManger sharedInstance] magnifierPoint_Moved:CGPointMake(self.selectBut.width/2, self.selectBut.height/2) withView:self.selectBut];
break;
}
case UIGestureRecognizerStateEnded:
case UIGestureRecognizerStateCancelled:
case UIGestureRecognizerStateFailed:
{
self.selectBut = nil;
[[ASMagnifierManger sharedInstance] magnifier_Ended];
if (self.modifyCallBlock) {
self.modifyCallBlock(YES);
}
break;
}
default:
break;
}
[pan setTranslation:CGPointZero inView:self];
}
- (void)getCurSelectBut:(CGPoint)currentPoint {
UIButton *selectBut = _pointA;
CGFloat minLent = [self getLinethPointOne:currentPoint pointTwo:_pointA.center];
CGFloat nlent = [self getLinethPointOne:currentPoint pointTwo:_pointB.center];
if (nlent < minLent) {
minLent = nlent;
selectBut = _pointB;
}
nlent = [self getLinethPointOne:currentPoint pointTwo:_pointC.center];
if (nlent < minLent) {
minLent = nlent;
selectBut = _pointC;
}
nlent = [self getLinethPointOne:currentPoint pointTwo:_pointD.center];
if (nlent < minLent) {
minLent = nlent;
selectBut = _pointD;
}
nlent = [self getLinethPointOne:currentPoint pointTwo:_pointE.center];
if (nlent < 60) {
minLent = nlent;
selectBut = _pointE;
}
nlent = [self getLinethPointOne:currentPoint pointTwo:_pointF.center];
if (nlent < 60) {
minLent = nlent;
selectBut = _pointF;
}
nlent = [self getLinethPointOne:currentPoint pointTwo:_pointG.center];
if (nlent < 60) {
minLent = nlent;
selectBut = _pointG;
}
nlent = [self getLinethPointOne:currentPoint pointTwo:_pointH.center];
if (nlent < 60) {
minLent = nlent;
selectBut = _pointH;
}
self.selectBut = selectBut;
}
//测量两点的直线距离
- (CGFloat)getLinethPointOne:(CGPoint)oneP pointTwo:(CGPoint)twoP {
CGFloat lenth = sqrt(pow(oneP.x - twoP.x, 2) + pow(oneP.y - twoP.y, 2));
return lenth;
}
网友评论