2

我正在尝试使用 customCALayer内部的 custom来实现 UI 元素UIView

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

尽管滑块确实按预期在视觉上旋转,但当我对收到的触摸执行命中测试时,“可命中”区域仍位于滑块的预旋转位置。就好像CAShapeLayer' 的旋转的视觉效果与UIBezierPath嵌入在图层内部以形成图层path属性的 ' 解耦,因为我正在使用该路径CGPathContainsPoint()来识别滑块上的触摸。

一般来说,我是 Core Graphics 的新手,所以我想可能有一个属性我在这里设置不正确,但我很难弄清楚它是什么。

这是代码:

可触摸图层.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.
    }
}

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

4

1 回答 1

3

我通常希望事件位置以它们被传递到的 UIView 表示。由于_slider相对于它的超层(即 UIView 的支持层)有一个转换,因此您要使用的任何几何值都需要转换为该参考框架。简而言之,您需要将点显式转换为_slider' 参考系。尝试这样的事情:

-(BOOL) checkSliderTouchAtPoint: (CGPoint) point
{
    CGPoint pointInSliderLayer = [_slider convertPoint: point fromLayer: self.layer];
    return CGPathContainsPoint(_slider.path , NULL, pointInSliderLayer, NO);
}
于 2013-07-17T12:32:05.010 回答