圆形拖动在大圆的范围内,但是效果总是达不到,求大神们帮忙看看,万分感谢
- (void)viewDidLoad {
[superviewDidLoad];
// 处理图片为圆形
[self.coronacircleView];
[self.rockercircleView];
// 指定图片可以处理事件
[self.rockersetUserInteractionEnabled:YES];
[self.coronasetWidth:150];
[self.coronasetHeight:150];
// 创建pan(拖动)手势
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizeralloc] initWithTarget:selfaction:@selector(handlePan:)];
[self.rocker addGestureRecognizer:panRecognizer];
panRecognizer.delegate = self;
panRecognizer.maximumNumberOfTouches = 1;
self.rocker.center = self.corona.center;
}
- (void)handlePan:(UIPanGestureRecognizer *)recognizer
{
switch (recognizer.state) {
caseUIGestureRecognizerStateEnded:
{
recognizer.view.center = self.corona.center;
}
break;
caseUIGestureRecognizerStateBegan:
caseUIGestureRecognizerStateChanged:
{
UIWindow *window = [[UIApplicationsharedApplication] keyWindow];
CGPoint bigCircle = [self.corona.superview convertPoint:self.corona.center toView:window];
CGPoint smallCircle = [self.rocker.superview convertPoint:self.rocker.center toView:window];
CGFloat distanceX = bigCircle.x - smallCircle.x;
CGFloat distanceY = bigCircle.y - smallCircle.y;
// 圆心距
CGFloat circleDistance = distanceX * distanceX + distanceY * distanceY;
// 半径差
CGFloat bigR = self.corona.width / 2;
CGFloat smallR = self.rocker.width / 2;
CGFloat circleDistance2 = (bigR - smallR) * (bigR - smallR);
CGPoint translation = [recognizer translationInView:self.corona];
if(circleDistance>=0 && circleDistance<circleDistance2)
{
[recognizer setTranslation:CGPointZero inView:self.corona];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
}
if(distanceX >= smallR)
{
NSLog(@"left");
}
if(distanceX <= -smallR)
{
NSLog(@"right");
}
if(distanceY >= smallR)
{
NSLog(@"top");
}
if(distanceY <= -smallR)
{
NSLog(@"down");
}
}
break;
default:
break;
}
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
returnYES;
}
有点难度。