PDA

View Full Version : Rotating UIImage view then rotate in opposite direction based on condition




nexus74
Jul 17, 2010, 07:30 AM
Hello,

I'm new to objective c. My code has a scrolling back ground and in the fore ground is a spinning wheel.

I can scroll the back ground and rotate the wheel UIImage view just fine.

I'm having an issue with making the wheel rotate in the opposite direction when my background begins to scroll in the opposite direction.

Here's my code:

- (void) animateBG:(NSTimer *)animBGTimer {


scrollingBG.center = CGPointMake(scrollingBG.center.x, scrollingBG.center.y + scrollingBGMovement.y);


wheel.transform = CGAffineTransformRotate(wheel.transform, M_PI / 20.0f);

if (scrollingBG.center.y > 1220 || scrollingBG.center.y < -770 )
scrollingBGMovement.y = -scrollingBGMovement.y;

if (scrollingBG.center.y > 1220){
wheel.transform = CGAffineTransformRotate(wheel.transform, M_PI / -20.0f);
}
}

I've tried putting the line
wheel.transform = CGAffineTransformRotate(wheel.transform, M_PI / -20.0f);
right under my first "if" statement, but no go.

Any ideas?

Thank you
Michael



chown33
Jul 17, 2010, 12:16 PM
In your head, step through what you're doing to your transform.

You apply a rotation of pi/20, then you conditionally apply a rotation of pi/-20. What is the net effect of rotating pi/20 followed by pi/-20?

nexus74
Jul 17, 2010, 02:41 PM
I see what you're saying, but using -40f does not work either. What does the "f" in -40f represent ?

robbieduncan
Jul 17, 2010, 02:57 PM
I see what you're saying, but using -40f does not work either. What does the "f" in -40f represent ?

If you don't know what the f means why have you been using it? Did you just blindly copy and paste the code from somewhere?

This is why I always tell people to learn C before Objective-C. For your reference the answer can be found here (http://en.wikipedia.org/wiki/C_syntax#Floating_point_types).

chown33
Jul 17, 2010, 06:01 PM
I see what you're saying, but using -40f does not work either.

Post your revised code.

Also identify the types of your variables: scrollingBG, scrollingBGMovement, etc. I think you have a logic failure, but without seeing what the data types are, it's impossible to guess.

I see no code that corresponds to detecting a reversal of direction. "Direction" is either a positive or negative change in position, but I don't see anything that's calculating a change in position. I see calculations that involve a range of positions, but range has nothing to do with direction. You might want to walk through your calculations manually for a scrolling up, and then again for a scrolling down. Assume the position changes by 10 in each direction.

This code makes no sense:
if (scrollingBG.center.y > 1220 || scrollingBG.center.y < -770 )
scrollingBGMovement.y = -scrollingBGMovement.y;

Think about what happens to scrollingBGMovement.y when the method is executed 4 times in a row when scrollingBG.center.y > 1220 or scrollingBG.center.y < -770 .

nexus74
Jul 18, 2010, 11:50 AM
Thank you all for your responses.

As far as the line that doesn't make sense:

if (scrollingBG.center.y > 1220 || scrollingBG.center.y < -770 )
scrollingBGMovement.y = -scrollingBGMovement.y;

Whatever direction the scrollingBG happens to be moving, go in the opposite directions when the center.y of that scrollingBG reaches the above indicated values.

here is my ...ViewController.h file:
#import <UIKit/UIKit.h>

@interface SideScrollerViewController : UIViewController {

UIImageView *scrollingBG;
CGPoint scrollingBGMovement;

UIImageView *wheel;

}

@property (nonatomic, retain) IBOutlet UIImageView *scrollingBG;
@property (nonatomic, retain) IBOutlet UIImageView *wheel;



- (void) initializeTimer;
- (void) animateBG: (NSTimer *)animBGTimer;


@end

Here is my ...ViewController.m file:
#import "SideScrollerViewController.h"

@implementation SideScrollerViewController

@synthesize scrollingBG;
@synthesize wheel;

- (void)dealloc {

[scrollingBG release];
[wheel release];
[super dealloc];
}

- (void) initializeTimer {
float bgScrollInterval = 1.0 / 60.0;
[NSTimer scheduledTimerWithTimeInterval:bgScrollInterval target:self selector:@selector(animateBG:) userInfo:nil repeats:YES];
}

- (void) animateBG:(NSTimer *)animBGTimer {


scrollingBG.center = CGPointMake(scrollingBG.center.x, scrollingBG.center.y + scrollingBGMovement.y);

//rotate wheel
wheel.transform = CGAffineTransformRotate(wheel.transform, M_PI / 20);


if (scrollingBG.center.y > 1220 || scrollingBG.center.y < -770 )
scrollingBGMovement.y = -scrollingBGMovement.y;

//reverse wheel rotation (doesn't work)
wheel.transform = CGAffineTransformRotate(wheel.transform, M_PI / -40);

}

- (void)viewDidLoad {
[super viewDidLoad];

scrollingBGMovement = CGPointMake(0, 4);

[self initializeTimer];

}


Aside from getting the wheel to reverse direction, everything works.

Within the "If" statement is where I was origninally attempting to cause the wheel to rotate in the opposite direction that it is currently rotating in order to match the scrolling background when it changes direction.

chown33
Jul 18, 2010, 01:12 PM
This line:
wheel.transform = CGAffineTransformRotate(wheel.transform, M_PI / -40);

is not in the body of an 'if' statement. It's always executed. That makes little sense given the earlier rotation by pi/20.

You really need to step back from the coding and work through the algorithm's logic again. You're doing several things wrong, and you need to fix several things before it will work. Simply hacking at it will not make it work.