Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to alloc initWithCoder or initWithFrame

    #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.

like image 480
John Riselvato Avatar asked Mar 02 '26 09:03

John Riselvato


1 Answers

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.

like image 89
Oliver Avatar answered Mar 07 '26 11:03

Oliver