检测旋转CAShapeLayer上的触摸

时间:2013-07-16 14:20:59

标签: ios uiview core-graphics calayer cashapelayer

我正在尝试使用自定义CALayer内的自定义UIView来实现UI元素。

基本上,用户用他/她的手指围绕圆圈移动滑块,因此我有一个用于交互的图层,然后是一个CAShapeLayer类型的子图层,它代表滑块本身。我认为围绕圆圈移动滑块的最简单方法是围绕其z轴旋转CAShapeLayer

虽然滑块确实按预期进行了视觉旋转,但是当我对接收到的触摸执行命中测试时,“可击打”区域仍然位于滑块的预旋转位置。就好像CAShapeLayer的旋转的视觉效果与嵌入图层内部的UIBezierPath分离,形成图层的path属性,因为我正在使用该路径与CGPathContainsPoint()一起识别滑块上的触摸。

我一般都是Core Graphics的新手,所以我想可能有一个属性我在这里没有正确设置,但我很难弄明白它是什么。

以下是代码:

TouchableLayer.m

@interface TouchableLayer ()
{
    CAShapeLayer *_slider;    // The interactive slider that gets moved around the circle.
}

-(id) initWithPosition:(NSInteger) position // Designated initializer for this layer.
{
    if ( self = [super init] )
    {
        _slider = [CAShapeLayer layer];

        _slider.fillColor = [UIColor blackColor].CGColor;

        [self addSublayer:_slider];
    }

    return self;
}

-(void) setFrame:(CGRect)frame
{
    [super setFrame:frame];

    _slider.frame = frame;

    // This path is currently hardcoded to be in the right starting spot according to
    //    other UI elements, but the magic numbers will go away once I figure out this
    //    rotation issue.
    _slider.path = [UIBezierPath bezierPathWithRect:CGRectMake(self.bounds.size.width-47, self.bounds.size.height/2-5, 30.0f, 10.0f)].CGPath;

}

// Checks if the given touch location was on the slider. Returns YES if it was and NO if it was not.
-(BOOL) checkSliderTouchAtPoint: (CGPoint) point
{
    if (CGPathContainsPoint(_slider.path , NULL, point, NO))
    {
        return YES;
    }
    else
    {
        return NO;
    }
}

// Purpose: Takes the given touch location, determines the angle (in radians) that it forms with respect the center of the screen,
//             and returns that angle on the interval [0-2pi] radians. [0-2pi] radians follows a positive counterclockwise path.
-(double) angleForTouchPoint:(CGPoint) point
{
    // We use the positive counterclockwise coordinate system in the drawing code since that's what's used traditionally
    //    outside of Apple's APIs, so multiplying the result of
    //    atan2() by -1 converts the angle from a positive clockwise unit circle to a positive counterclockwise
    //    unit circle.

    double angleInRadians = -1*atan2(point.y - (self.frame.size.height/2), point.x - self.frame.size.width/2);

    if (angleInRadians < 0) // Convert angle to 0 - 2pi radians; we want non-negative angles.
    {
        angleInRadians += M_PI*2;
    }

    return angleInRadians;
}
// points get fed into this from the UIView.
-(void) updateWithTouchAtPoint:(CGPoint) point
{
    if ([self checkSliderTouchAtPoint:point])
    {
        double touchAngle = [self angleForTouchPoint:point];
        _slider.transform = CATransform3DMakeRotation(-M_PI, 0.0, 0.0, 1.0); // Hardcoded angle rotation for now since I need to do some subtraction later in order to determine the amount to rotate based on touches.
    }
}

我非常感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:3)

我通常希望事件位置以他们被传递到的UIView表示。由于_slider相对于它的超级层(即UIView的背衬层)有变换,因此需要将要使用的任何几何值转换为该参照系。简而言之,您需要将该点明确转换为_slider的参照系。尝试这样的事情:

-(BOOL) checkSliderTouchAtPoint: (CGPoint) point
{
    CGPoint pointInSliderLayer = [_slider convertPoint: point fromLayer: self.layer];
    return CGPathContainsPoint(_slider.path , NULL, pointInSliderLayer, NO);
}