我使用CGContextAddArc创建了“ball”。我的想法是为它添加点击和平移手势。
- (void)drawBallAtPoint:(CGPoint)centerPoint inContext:(CGContextRef)context {
UIGraphicsPushContext(context);
CGContextBeginPath(context);
CGContextMoveToPoint(context, centerPoint.x,centerPoint.y);
CGContextAddArc(context, centerPoint.x, centerPoint.y, 30, 0.0, 2*M_PI, NO); CGContextFillPath(context);
UIGraphicsPopContext();
}
我知道如何判断当前触摸的是“球”,但这只适用于轻拍手势。
- (BOOL)isPointInBall:(CGPoint)point {
CGRect ballTouchRect = CGRectMake(self.ballCenterPoint.x - kBallRadius, self.ballCenterPoint.y - kBallRadius, kBallRadius*2, kBallRadius*2);
return CGRectContainsPoint(ballTouchRect, point);
}
在panGesture中它无法正常工作
- (void)panGestureHappened:(UIPanGestureRecognizer *)panGestureRecognizer {
CGPoint tapLocation = [panGestureRecognizer locationInView:self];
if ([self isPointInBall:tapLocation]) {
self.canBETouched = YES;
NSLog(@"START");
}
switch (panGestureRecognizer.state) {
case UIGestureRecognizerStateBegan:{
self.touched = YES;
NSLog(@"BEGAN");
break;
}
case UIGestureRecognizerStateChanged: {
if (self.canBETouched) {
CGFloat radius = [self sliderRadius];
CGPoint sliderCenter = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
CGPoint sliderStartPoint = CGPointMake(sliderCenter.x, sliderCenter.y - radius);
CGFloat angle = angleBetweenThreePoints(sliderCenter, sliderStartPoint, tapLocation);
if (angle < 0) {
angle = -angle;
}
else {
angle = 2*M_PI - angle;
}
self.value = translateValueFromSourceIntervalToDestinationInterval(angle, 0, 2*M_PI, self.minimumValue, self.maximumValue);
}
break;
}
case UIGestureRecognizerStateEnded:{
self.touched = NO;
self.canBETouched = NO;
break;
}
default:
break;
}
}
它只有作用,如果触摸从“球”开始然后然后它在“球”内滑动。如果触摸开始于“球”,它就不会响应。
那么,我怎样才能在这个球上添加panGesture?