2
    #import "DotAnimation.h"


@implementation DotAnimation
DoodlePad* dp;

-(void)setupView {
    Ball = CGRectMake(10, 10, 10, 10);
    [NSTimer scheduledTimerWithTimeInterval:(1.0f / 30.0f) target:self selector:@selector(traceBall) userInfo:nil repeats:YES];
}

-(id)initWithCoder:(NSCoder *)aDecoder {
    if (self == [super initWithCoder:aDecoder]){
        [self setupView];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
    }
    return self;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.*/
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [[UIColor yellowColor] CGColor]);
    CGContextFillEllipseInRect(context, Ball);

}
-(void)traceBall{
    vector<CGPoint>::iterator L = dp.doodlePoints->begin();
    while(L != dp.doodlePoints->end()){
        Ball.origin.x += (*L).x;
        Ball.origin.y += (*L).y;
        ++L;
    }
    [self setNeedsDisplay];
}

- (void)dealloc
{
    [super dealloc];
}

@end

I have this Animation I am trying to use in another file. So i figure i do something like

trace = [[DotAnimation alloc]initWithCoder];

or

trace = [[DotAnimation alloc]initWithFrame];

I am unsure which one to use or if i am even writing this correctly.

I want to be able to use:

    while(k != dp.doodlePoints->end()){
    Ball.origin.x += (*k).x;
    Ball.origin.y += (*k).y;
    ++k;
}

In another file but I don't know how to call Ball.origin from DotAnimation

Also it would be great if you could link me to good information on understanding this.

1
  • 1
    Anyone reading this. I suggest you don't follow the way I am trying to do animation. Please read this for proper animation. apeth.com/iOSBook/ch17.html Commented Nov 9, 2011 at 19:39

2 Answers 2

15

initWithCoder is called by the framework when you embed an object into a XIB. Like a ViewController for example. So you should not call it by yourself.

Calling initWithFrame could be a better solution if you want to set the frame (like that seems to be done here). But you may give a frame, and add a subview created by yourself.

if you don't want to create the subview by yourself, then it's initWithNibName:bundle: that you may call/override. Creating the XIB that contains the view. But you will still need to add that view as subview to the main view that is showing that animated view.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for describing these two to me.
2

initWithCoder: is something you would call yourself when restoring from an archive; it isn't appropriate for something which appears to be a view object. As it is only used for simple setup code, you can remove it.

Also, animating by changing the "Ball" object from another object isn't really consistent with Cocoa view programming. You should take a look at "Introduction to View Programming Guide for Cocoa" in the documentation, and work from there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.