How To Move UIImageView? - animation

Heres the deal:
I code an UIImageView in the viewDidLoad and i want it to move down with my fonction buttonPressed** without creating an other subview like i do.
Heres the code.
-(void)viewDidLoad {
[super viewDidLoad];
banetteImage = [UIImage imageNamed:#"myBanette.png"];
UIImageView *banetteView = [[UIImageView alloc] initWithImage:banetteImage];
banetteView.frame = CGRectMake(100, -740, 568, 790);
banetteView.opaque = NO;
[NSTimer scheduledTimerWithTimeInterval:2.5 target:self selector:#selector(buttonPressed: ) userInfo:nil repeats:NO];
[self.view addSubview:banetteView];
}
-(void)buttonPressed {
double speed = 1 / round(random() % 100) + 1.0;
[UIView beginAnimations:nil context:banetteView];
[UIView setAnimationDuration: 2*speed ];
banetteView.frame = CGRectMake(100, -2, 568, 790);
banetteView.opaque = NO;
UIImageView *banetteView = [[UIImageView alloc] initWithImage:banetteImage];
banetteView2.frame = CGRectMake(100, -740, 568, 790);
banetteView.opaque = NO;
banetteView.hidden = YES;
[self.view addSubview:banetteView];
// set a stop callback so we can cleanup the banette when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:#selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
-(void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
UIImageView *banetteView = context;
double speed = 1 / round(random() % 100) + 1.0;
banetteView.frame = CGRectMake(100, -2, 568, 790);
banetteView2.opaque = NO;
[self.view addSubview:banetteView2];
[UIView beginAnimations:nil context:banetteView];
[UIView setAnimationDuration: 2*speed ];
[banetteView release];
}

I found by adapting this code
http://mobile.tutsplus.com/tutorials/iphone/iphone-sdk-learning-about-touch-events-basic-game-animation/

Related

infinitely scrolling background (UIView animation)

I'm trying to achieve an infinitely scrolling background by having two UIViews scroll and replace each other like a conveyor belt. This is my code so far, I can't seem to get it to work
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
bg1 = [[UIView alloc] initWithFrame:self.view.frame];
[bg1 setBackgroundColor:[UIColor redColor]];
bg2 = [[UIView alloc] initWithFrame:self.view.frame];
[bg2 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:bg1];
[self.view addSubview:bg2];
[self.view bringSubviewToFront:bg1];
[self animate];
}
- (void)animate {
[UIView animateWithDuration:3000 animations:^{
bg1.frame = CGRectOffset(bg1.frame, 0, bg1.frame.size.height);
}completion:^(BOOL done) {
if (done) {
bg1.frame = CGRectOffset(bg1.frame, 0, -bg1.frame.size.height);
[self.view sendSubviewToBack:bg1];
[UIView animateWithDuration:3000 animations:^{
bg2.frame = CGRectOffset(bg2.frame, 0, bg2.frame.size.height);
}completion:^(BOOL done) {
if (done) {
bg2.frame = CGRectOffset(bg2.frame, 0, -bg2.frame.size.height);
[self.view sendSubviewToBack:bg2];
[self animate];
}
}];
}
}];
}
my bad! i thought withDuration was in ms. It's in seconds!
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
bg1 = [[UIView alloc] initWithFrame:self.view.frame];
[bg1 setBackgroundColor:[UIColor redColor]];
bg2 = [[UIView alloc] initWithFrame:self.view.frame];
[bg2 setBackgroundColor:[UIColor blueColor]];
[self.view addSubview:bg1];
[self.view addSubview:bg2];
[self.view bringSubviewToFront:bg1];
[self animate];
}
- (void)animate {
[UIView animateWithDuration:3 animations:^{
bg1.frame = CGRectOffset(bg1.frame, 0, bg1.frame.size.height);
}completion:^(BOOL done) {
if (done) {
bg1.frame = CGRectOffset(bg1.frame, 0, -bg1.frame.size.height);
[self.view sendSubviewToBack:bg1];
[UIView animateWithDuration:3 animations:^{
bg2.frame = CGRectOffset(bg2.frame, 0, bg2.frame.size.height);
}completion:^(BOOL done) {
if (done) {
bg2.frame = CGRectOffset(bg2.frame, 0, -bg2.frame.size.height);
[self.view sendSubviewToBack:bg2];
[self animate];
}
}];
}
}];
}

Fading a UIView with a subviewed button

I'm trying to create a button that fades out and then in when I open the page.
Here is the current code that i'm using, it doesn't work to fade the buttons in/ out:
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
if (index == 0) {
view = [[[NSBundle mainBundle] loadNibNamed:#"nib1" owner:self options:nil] lastObject];
}else {
view = [[[NSBundle mainBundle] loadNibNamed:#"nib2" owner:self options:nil] lastObject];
UIView *containView = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 60, 100)];
_btn = [UIButton buttonWithType:UIButtonTypeCustom];
_btn.frame = CGRectMake(30, 90, 40, 30 );
[_btn setBackgroundImage:[UIImage imageNamed:#"playButton.png"] forState:UIControlStateNormal];
[_btn addTarget:self action:#selector(fadeButton:) forControlEvents:UIControlEventTouchUpInside];
//[self fadeOut:_btn withDuration:1.0 andWait:1.0];
[containView addSubview:_btn];
[containView setAlpha:0.0];
[view addSubview:containView];
[UIView beginAnimations:nil context:nil];
[containView setAlpha:1.0];
[UIView commitAnimations];
}
return view;
}
I've also tried using:
[UIView animateWithDuration:0.5 animations:^{_btn.alpha = 1.0;}];
None of those work. Does anyone have any tips on how to fade a subview out? Thanks
When you call [UIView commitAnimations], the animations are animating at the same time.
When you want to animate something after an other animation try to nest animation blocks with completion:
[UIView animateWithDuration:0.5 animations:^
{
//animate out
}
completion:^ (BOOL finished)
{
[UIView animateWithDuration:0.5 animations:^
{
//animate in
}
completion:^ (BOOL finished)
{
//Optional
}];
}];
To fade out, you have to animate the alpha to 0, not to 1.
[UIView animateWithDuration:0.5 animations:^{_btn.alpha = 0.0;}];
This should work.

How to create a flashing record button for iPhone

I've looked at several posts, including [this one].1 This is exactly what I want to do, but I cannot get it to work.
I want to show the user that they have pressed a record button, and that they will need to press it again to stop recording.
So far, the code for the start recording method looks like this:
NSLog(#"DetailVC - recordAudio - soundFilePath is %#", soundFile);
[audioRecorder prepareToRecord];
recording = YES;
NSLog(#"start recording");
NSLog(#"1");
//[autoCog startAnimating];
[audioRecorder recordForDuration:120];
recording = NO;
//Start a timer to animate the record images
if (recording == YES) {
toggle = FALSE;
timer = [NSTimer scheduledTimerWithTimeInterval: 2.0
target: self
selector: #selector(toggleButtonImage:)
userInfo: nil
repeats: YES];
//UIImage *changeImage = [UIImage imageNamed:stopButtonFile];
//[recordButton setImage:changeImage forState:UIControlStateNormal];
//[timer invalidate];
//timer = nil;
NSLog(#"2");
}
and the toggleButton method looks like this:
- (void)toggleButtonImage:(NSTimer*)timer
{
NSLog(#"%s", __FUNCTION__);
if(toggle)
{
NSLog(#"1");
/*
[UIView beginAnimations];
recordButton.opacity = 0.0;
[recordButton setAnimationDuration: 1.5];
[recordButton commitAnimations];
[recordButton setImage:[UIImage imageNamed:#"record.png"] forState: UIControlStateNormal];
*/
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; //delete "EaseOut", then push ESC to check out other animation styles
[UIView setAnimationDuration: 0.5];//how long the animation will take
[UIView setAnimationDelegate: self];
recordButton.alpha = 1.0; //1.0 to make it visible or 0.0 to make it invisible
[UIView commitAnimations];
}
else
{
NSLog(#"2");
CGContextRef context = UIGraphicsGetCurrentContext();
[UIView beginAnimations:nil context:context];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; //delete "EaseOut", then push ESC to check out other animation styles
[UIView setAnimationDuration: 0.5];//how long the animation will take
[UIView setAnimationDelegate: self];
recordButton.alpha = 0.0; //1.0 to make it visible or 0.0 to make it invisible
[UIView commitAnimations];
//[recordButton setImage:[UIImage imageNamed:#"stopGrey.png"] forState: UIControlStateNormal];
}
toggle = !toggle;
}
I get to see the logs showing that the loop is iterating, but:
the images do not switch, and
the operation is not running in parallel with the record method.
I would appreciate any ideas as to how to proceed.
Expanding on Ashley's answer, I'd go with something more like this:
- (IBAction)record:(id)sender {
self.recording = !self.recording;
if (!self.recording) {
[UIView animateWithDuration:0.1
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState
animations:^{
self.recordButton.alpha = 1.0f;
}
completion:^(BOOL finished){
}];
} else {
self.recordButton.alpha = 1.0f;
[UIView animateWithDuration:0.5
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionRepeat | UIViewAnimationOptionAutoreverse | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.recordButton.alpha = 0.0f;
}
completion:^(BOOL finished){
}];
}
}
Try this:
- (IBAction)record:(id)sender
{
self.recording = !self.recording;
[self toggleButton];
}
- (void) toggleButton
{
if (!self.recording) {
self.recordButton.alpha = 1.0;
return;
}
[UIView animateWithDuration: 0.5
delay: 0.0
options: UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
self.recordButton.alpha = !self.recordButton.alpha;
}
completion:^(BOOL finished) {
[self toggleButton];
}];
}

UITouch Event on Animated UIImagevIew

I am using the Snowfall sample code which drops UImages from the top of the screen. I want to detect a Touch on the UIImageview and update a label. If i create a single UIImageView in IBOutlet and connect it to the touch event the label is updated correctly. But when I try to apply it to the falling UIImageView it does not work. Here is what I have so far:
- (void)viewDidLoad {
[super viewDidLoad];
score = 0;
self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0];
flakeImage = [UIImage imageNamed:#"flake.png"];
[NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:#selector(onTimer) userInfo:nil repeats:YES];
}
- (void)onTimer
{
flakeView = [[UIImageView alloc] initWithImage:flakeImage];
flakeView.opaque = YES;
flakeView.userInteractionEnabled = YES;
flakeView.multipleTouchEnabled = YES;
int startX = round(random() % 320);
int endX = round(random() % 320);
double scale = 1 / round(random() % 100) + 1.0;
double speed = 1 / round(random() % 100) + 1.0;
flakeView.frame = CGRectMake(startX, -100.0, 25.0 * scale, 25.0 * scale);
[self.view addSubview:flakeView];
[self.view bringSubviewToFront:flakeView];
[UIView beginAnimations:nil context:flakeView];
[UIView setAnimationDuration:5 * speed];
flakeView.frame = CGRectMake(endX, 500.0, 25.0 * scale, 25.0 * scale);
[UIView setAnimationDidStopSelector:#selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == flakeView)
{
NSLog(#"tag %#",touch);
score = score + 1;
lbl1.text = [NSString stringWithFormat:#"%i", score];
}
}
Animations typically disable touch handling so you have to use:
animateWithDuration:delay:options:animations:completion
to set the options to receive touches.
http://developer.apple.com/library/IOs/#documentation/WindowsViews/Conceptual/ViewPG_iPhoneOS/AnimatingViews/AnimatingViews.html#//apple_ref/doc/uid/TP40009503-CH6-SW1
IIRC the option you want is: UIViewAnimationOptionAllowUserInteraction Look for UIViewAnimationOptions in the above doc for the full list.

Animation code stopped working

I have animation code in my AppDelegate file thats making the Default.png splashscreen to fade
out to the first controller of the app.
Since I implemented TabBar in my code, the animation stopped working.
Any idea why?
- (void) splashFade
{
splashView = [[UIImageView alloc]
initWithFrame:CGRectMake(0,0, 320, 480)];
splashView.image = [UIImage imageNamed:#"Default.png"];
[_window addSubview:splashView];
[_window bringSubviewToFront:splashView];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:2.0];
[UIView
setAnimationTransition:UIViewAnimationTransitionNone
forView:_window cache:YES];
[UIView setAnimationDelegate:self];
[UIView
setAnimationDidStopSelector:
#selector(startupAnimationDone:finished:context:)];
splashView.alpha = 0.0;
[UIView commitAnimations];
}
- (void)startupAnimationDone:(NSString *)animationID
finished:(NSNumber *)finished
context:(void *)context
{
[splashView removeFromSuperview];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
sleep(2);
[self splashFade];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
UIViewController *viewController1 = [[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil];
UIViewController *viewController2 = [[TableViewController alloc] initWithNibName:#"TableViewController" bundle:nil];
self.tabBarController = [[UITabBarController alloc] init];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
self.window.rootViewController = self.tabBarController;
[self.window addSubview:self.rootViewController.view];
[self.window addSubview:self.navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
Try using
[self performSelector:#selector(splashFade) withObject:nil afterDelay:2];
better than sleep(2);

Resources