tags:

views:

45

answers:

2

hi, i have done following to translate background screen.view.m file.but the frame rate is very slow.i changed time interval with different value,but the image is translating very slowly in Iphone Device? any solution?pls?

 - (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
    // Initialization code
    x =0;

    xx = 0;
    yy = 0;
    secX = 0;

    [NSTimer scheduledTimerWithTimeInterval:(0.1/60) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
return self;

}

 -(void) onTimer
 {  
xx++;
xx = ( xx % 320);

[self setNeedsDisplay];

}



 - (void)drawRect:(CGRect)rect {
// Drawing code


[[UIImage imageNamed:@"graphic.png"] drawAtPoint:CGPointMake(yy,xx )];

if(xx >= 0)
{

    [[UIImage imageNamed:@"graphic.png"]  drawAtPoint:CGPointMake((-320 - (-1 * xx)),yy)];


}
+2  A: 

Hi,

You seem to be drawing the image each time in your drawRect method. I think if you used a UIImageView to hold the UIImage and just moved that it might be faster?

In your .h file

@property (nonatomic, retain) UIImageView *myImageView;

In your init method

self.myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"graphic.png"]] autorelease];

and in your timer callback just move the UIImageView like so

- (void) onTimer {
    xx++;
    xx = ( xx % 320);

    self.myImageView.center = CGPointMake(xx, self.myImageView.center.y);
}

(There is no need for a drawRect anymore)

Hope that helps,

Sam

deanWombourne
but i want to draw in DrawRect method for some reason...why NSTimer did not support it?
Mikhail Naimy
Even so, you're still creating a UIImage each frame, it might help to store the UIImage as a property and create it in the init method. What reason do you have for drawing in the drawInRect method that can't be fixed with the solution above?
deanWombourne
If you still want to use -drawRect: to lay out your content initially, that's fine. You will still want to move the view with the custom content using Core Animation. You might even want to rethink the use of the timer, instead just creating an animation with start and end points for the path you want the image to travel. Redrawing the content within a view or layer is an expensive operation, and should be avoided when possible. Core Animation is completely hardware accelerated.
Brad Larson
how will you crop UImage(which as sprite) and redraw again for gameapplication without redrawing Content?pls?
Mikhail Naimy
+1  A: 

NSTimer is working fine. Your problem is that drawing an image is a relatively processor intensive task and you are doing it 600 times a second. You're simply swamping the relatively slow processor on the iPhone.

You need to follow the previous advice and use an imageview or move the image to its own CALayer within the view. That way, you only draw the image once and then you can draw anything else in other layers.

Alternatively, you can create a UIImageView and then set other views as subviews. You then translate the imageview and draw into the subviews.

I can't think of any reason why drawing the image over and over again 600/sec in drawrect is the best way to go.

TechZen