iosxcodecs193puidynamicanimator

Why my class Inheritance from UIDynamicAnimator can not use addChildBehavior


I am learning ios with Stanford CS193P, and have issue in Lecture 8

I created a class named DropItBehavior inheritance from UIDynamicAnimator, but I can not use a UIDynamicAnimator method addChildBehavior,xcode warning like"No visible @interface for "DropItBehavior" declares the selector 'addChildBehavior'"

Here is the .h file

#import <UIKit/UIKit.h>

@interface DropItBehavior : UIDynamicAnimator

- (void)addItem:(id <UIDynamicItem>)item;
- (void)removeItem:(id <UIDynamicItem>)item;

@end

Here is the .m file

#import "DropItBehavior.h"

@interface DropItBehavior()
@property (strong, nonatomic) UIGravityBehavior *gravity;
@property (strong, nonatomic) UICollisionBehavior *collision;
@end

@implementation DropItBehavior

- (instancetype)init
{
    self = [super init];
    [self addChildBehavior:self.gravity];//Here is the Xcode warning"No visible @interface for "DropItBehavior" declares the selector 'addChildBehavior"
    return self;
}
- (instancetype)init
{
    self = [super init];
    [self addChildBehavior:self.gravity];
    return self;
}
- (UIGravityBehavior *)gravity
{
    if (!_gravity) {
        _gravity = [[UIGravityBehavior alloc] init];
        _gravity.magnitude = 0.9;
    }
    return _gravity;
}

- (UICollisionBehavior *)collision
{
    if (!_collision) {
        _collision = [[UICollisionBehavior alloc] init];
        _collision.translatesReferenceBoundsIntoBoundary = YES;
    }
    return _collision;
}

- (void)addItem:(id <UIDynamicItem>)item
{
    [self.gravity addItem:item];
    [self.collision addItem:item];

}

- (void)removeItem:(id <UIDynamicItem>)item
{
    [self.gravity removeItem:item];
    [self.collision removeItem:item];
}



@end

Solution

  • This is because addChildBehavior is a method of UIDynamicBehavior, not of UIDynamicAnimator. According to CS-193P's lecture notes, your DropIt class should inherit from UIDynamicBehavior instead:

    @interface DropItBehavior : UIDynamicBehavior