【问题标题】:No visible @interface for 'UIAttachmentBehavior' declares the selector initWithItem in DynamicsCatalog.xcodeproj'UIAttachmentBehavior' 没有可见的@interface 在 DynamicsCatalog.xcodeproj 中声明选择器 initWithItem
【发布时间】:2013-09-26 02:11:31
【问题描述】:

我刚从苹果开发者门户下载 DynamicsCatalog.xcodeproj 但我收到了这个错误:

“UIAttachmentBejavior”没有可见的@interface 会清除选择器 initwititem

在这一行:

UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.square1 point:attachmentPoint attachedToAnchor:squareCenterPoint];

在 APLAttachmentsViewController.m 中

这是该行的上下文:

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIDynamicAnimator* animator = [[UIDynamicAnimator alloc] initWithReferenceView:self.view];
    UICollisionBehavior* collisionBehavior = [[UICollisionBehavior alloc] initWithItems:@[self.square1]];

    CGPoint squareCenterPoint = CGPointMake(self.square1.center.x, self.square1.center.y - 100.0);
    CGPoint attachmentPoint = CGPointMake(-25.0, -25.0);
    /*
     By default, an attachment behavior uses the center of a view. By using a small offset, we get a more interesting effect which will cause the view to have rotation movement when dragging the attachment.
     */
    UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.square1 point:attachmentPoint attachedToAnchor:squareCenterPoint];

    collisionBehavior.translatesReferenceBoundsIntoBoundary = YES;

    // Show visually the attachment points
    self.redSquare.center = attachmentBehavior.anchorPoint;
    self.blueSquare.center = CGPointMake(25.0, 25.0);

    [animator addBehavior:attachmentBehavior];
    self.animator = animator;

    self.attachmentBehavior = attachmentBehavior;
}

你们中有人知道为什么或如何修复这个演示代码吗?

【问题讨论】:

  • initWitItem,是错字吗?错过h。
  • 请贴出实际代码和实际错误信息。

标签: iphone ios ipad xcode5


【解决方案1】:

看看class reference for UIAttachmentBehavior:initWithItem:point:attachedToAnchor: 没有声明,它似乎在某个时候被删除了。使用其他初始化程序之一:

– initWithItem:attachedToAnchor:
– initWithItem:attachedToItem:
– initWithItem:offsetFromCenter:attachedToAnchor:
– initWithItem:offsetFromCenter:attachedToItem:offsetFromCenter:

您使用的选择器实际上已重命名为initWithItem:offsetFromCenter:attachedToAnchor:

【讨论】:

  • 我应该在 .h 文件上声明还是应该将其切换到 initWithItem:offsetFromCenter:attachedToAnchor: ?
  • 你应该使用initWithItem:offsetFromCenter:attachedToAnchor:。
【解决方案2】:

似乎示例代码使用了在 iOS 7 最终版本之前被替换的选择器。替换选择器是 initWithItem:offsetFromCenter:attachedToAnchor:。此外,该方法将UIOffset 作为第二个参数(而不是CGPoint)。

这是我使用的代码:

UIOffset attachmentOffset = UIOffsetMake(-25, -25);
UIAttachmentBehavior *attachmentBehavior = [[UIAttachmentBehavior alloc] initWithItem:self.square1 offsetFromCenter:attachmentOffset attachedToAnchor:squareCenterPoint];

attachmentOffset 替换 attachmentPoint。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-27
    • 2021-09-22
    相关资源
    最近更新 更多