缺少UIView的anchorPoint属性

zlo*_*log 6 iphone objective-c uiview ios

我正在使用来自apple的Touches代码的略微改编的代码(我刚刚将片段的变量名称更改为图像):

- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
        UIView *image = gestureRecognizer.view;
        CGPoint locationInView = [gestureRecognizer locationInView:image];
        CGPoint locationInSuperview = [gestureRecognizer locationInView:image.superview];

        // Gives error: Property 'anchorPoint' not found on object of type 'CALayer *'
        //image.layer.anchorPoint = CGPointMake(locationInView.x / image.bounds.size.width, locationInView.y / image.bounds.size.height);
        // Gives warning: Method '-setAnchorPoint' not found
        [image.layer setAnchorPoint:CGPointMake(locationInView.x / image.bounds.size.width, locationInView.y / image.bounds.size.height)];
        image.center = locationInSuperview;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,如注释中所述,image.layer.anchorPoint无法编译,但错误是无法找到'anchorPoint'属性.它在使用消息传递重写行时进行编译,但它仍会发出警告.

直接复制和粘贴Touches代码而不更改名称更改会产生相同的错误.编译Touches代码时也不会出现这些错误.

为什么是这样?

omz*_*omz 22

您可能没有包含需要使用的QuartzCore框架CALayer.

你需要添加

#import <QuartzCore/QuartzCore.h>
Run Code Online (Sandbox Code Playgroud)