首页 新闻 搜索 专区 学院

圆形摇杆功能,大圆套小圆

0
悬赏园豆:50 [待解决问题]

圆形拖动在大圆的范围内,但是效果总是达不到,求大神们帮忙看看,万分感谢

 

 

- (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;

}

jay_zhang的主页 jay_zhang | 初学一级 | 园豆:111
提问于:2015-05-30 14:40
< >
分享
所有回答(1)
0

有点难度。

Firen | 园豆:5483 (大侠五级) | 2015-06-01 07:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册