infinitely scrolling background (UIView animation) - xcode

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];
}
}];
}
}];
}

Related

Enters the method but does not display anything

I am trying to call a method to ViewController class form AppDelegate, it enter to the method but the problem is that is not displaying anything , I was looking for, but I canĀ“t find the answer. Some one could help me? thanks
this is the code :
AppDelegate.m
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if (mainDelegate.isflagON=TRUE)
{
ViewController *vc=[ViewController new];
[vc pause];
}
}
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
AppDelegate *mainDelegate = [[UIApplication sharedApplication] delegate];
if (mainDelegate.isflagON==false)
{
mainDelegate.isflagON=true;
CGSize viewSize = self.view.bounds.size;
// Add this line
GameScene *scene = [[GameScene alloc] initWithSize:viewSize];
scene.scaleMode = SKSceneScaleModeAspectFill;
self.scene = scene;
// Configure the view.
skView = (SKView *)self.view;
skView.showsFPS = YES;
skView.showsNodeCount = YES;
// Present the scene.
[skView presentScene:scene];
}else
{
[self doPause];
}
}
-(void) imprime
{
[self doPause];
}
- (void)doPause {
CGRect myImageRect = CGRectMake(0.0f, 0.0f, 200.0f, 200.0f);
backImageView = [[UIImageView alloc] initWithFrame:myImageRect];
[backImageView setImage:[UIImage imageNamed:#"dialogBox.png"]];
[self.view addSubview:backImageView];
[self.view sendSubviewToBack:backImageView];
//set the position of the button
buttonContinuar = [UIButton buttonWithType:UIButtonTypeCustom];
[buttonContinuar setBackgroundImage:[UIImage imageNamed:#"dialogButton.png"]
forState:UIControlStateNormal];
buttonContinuar.frame = CGRectMake(40.0f, 50.0f, 120, 35);
[buttonContinuar addTarget:self action:#selector(buttonWasClicked:) forControlEvents:UIControlEventTouchUpInside];
[buttonContinuar setTitle:#"Continuar" forState:UIControlStateNormal];
//add the button to the view
[self.view addSubview:buttonContinuar];
buttonMapa = [UIButton buttonWithType:UIButtonTypeCustom];
//[button setTitle:#"Continuar" forState:UIControlStateNormal];
[buttonMapa setBackgroundImage:[UIImage imageNamed:#"dialogButton.png"]
forState:UIControlStateNormal];
buttonMapa.frame = CGRectMake(40.0f, 85.0f, 120, 35);
[buttonMapa addTarget:self action:#selector(buttonMapaClicked:) forControlEvents:UIControlEventTouchUpInside];
[buttonMapa setTitle:#"Mapa" forState:UIControlStateNormal];
//add the button to the view
[self.view addSubview:buttonMapa];
buttonMenu = [UIButton buttonWithType:UIButtonTypeCustom];
//[button setTitle:#"Continuar" forState:UIControlStateNormal];
[buttonMenu setBackgroundImage:[UIImage imageNamed:#"dialogButton.png"]
forState:UIControlStateNormal];
buttonMenu.frame = CGRectMake(40.0f, 120.0f, 120, 35);
[buttonMenu addTarget:self action:#selector(buttonMenuClicked:) forControlEvents:UIControlEventTouchUpInside];
[buttonMenu setTitle:#"Menu" forState:UIControlStateNormal];
//add the button to the view
[self.view addSubview:buttonMenu];
}
You can try this
- (void)applicationWillEnterForeground:(UIApplication *)application
{
if (mainDelegate.isflagON=TRUE)
{
ViewController *vc=[[ViewController alloc]init];
[vc pause];
}
}

Custom UIBarButton not appearing correctly in UITableView

I have code that successfully creates a custom UIBarButtonItem that appears on the navigation bar. I use the code in other view controllers, and it works perfectly, but in the UITableViewController, the button appears as the regular BackButtonItem. Can someone help me out? Here is the code that I use in the viewDidLoad method in all of the viewControllers
#implementation P2OListViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = YES;
UIImage *backButtonImage = [UIImage imageNamed:#"button-back#2x.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 60, 35);
[backButton addTarget:self
action:#selector(goToMenu:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.backBarButtonItem = back;
- (void)viewWillAppear:(BOOL)animated {
}
- (void)goToMenu:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
hide the default backBarButtonItem by setting its hide property to yes
self.navigationItem.hidesBackButton = YES;
in your viewWillAppear
- (void)viewWillAppear:(BOOL)animated
{
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = YES;
UIImage *backButtonImage = [UIImage imageNamed:#"button-back#2x.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:backButtonImage forState:UIControlStateNormal];
backButton.frame = CGRectMake(0, 0, 60, 35);
[backButton addTarget:self
action:#selector(goToMenu:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *back = [[UIBarButtonItem alloc]initWithCustomView:backButton];
self.navigationItem.backBarButtonItem = back;
}
if above not works try this & see what happens:
if (!self.navigationItem.backBarButtonItem.hidden) {
self.navigationItem.backBarButtonItem.hidden = YES;
}
else {
NSLog(#"back button already hidden");
}

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 do I implement UIScrollView programatically

I am launching detail pages from a UITableView and within that detail page there is an image. I would like it to be zoomable, so I'd like the contents of that detail page to sit inside a scrollView. So far no luck, the labels, textviews and images are fine, but I can't get the ScrollView implemented.
here is my .m: THANKS!
- (void)viewDidLoad {
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scrollView.contentSize = CGSizeMake(320, 700);
scrollView.clipsToBounds = YES;
super.view.backgroundColor = [UIColor whiteColor];
[scrollView setUserInteractionEnabled:YES];
self.navigationItem.title = [NSString stringWithFormat:selectedCellItem];
CGRect frame = CGRectMake(0, 0, 10, 10);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.backgroundColor = [UIColor whiteColor];
frame = CGRectMake(0, 320, 10, 10);
UILabel *label = [[UILabel alloc] initWithFrame:frame];
//label.text = selectedCellItem;
label.font=[UIFont fontWithName:#"Verdana-Bold" size:20.0];
label.textAlignment = UITextAlignmentCenter;
[self.view addSubview:label];
[label release];
frame = CGRectMake(0, 320, 320, 60); //Description
UITextView *labelDesc = [[UITextView alloc] initWithFrame:frame];
labelDesc.text = selectedCellDesc;
labelDesc.font=[UIFont fontWithName:#"Verdana" size:12.0];
labelDesc.textAlignment = UITextAlignmentLeft;
//labelDesc.textAlignment = UITextAlignmentTop;
[self.view addSubview:labelDesc];
[labelDesc release];
frame = CGRectMake(0, 0, 320, 320); //Image
UIImageView *imageItem = [[UIImageView alloc] initWithFrame:frame];
[imageItem setImage:[UIImage imageNamed:selectedCellImage]];
// GET THIS ONE WORKING RIGHT>>[imageItem setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#.png", [[NSBundle mainBundle] bundlePath], selectedCellImage]]]; //use
//imageItem = [[UIImageView alloc] initWithContentsOfFile:[NSString stringWithFormat:#"shirt.png", [[NSBundle mainBundle] bundlePath]]];
//labelDesc.font=[UIFont fontWithName:#"Zapfino" size:13.0];
//labelDesc.textAlignment = UITextAlignmentCenter;
imageItem.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageItem];
//[imageItem release];
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
I think that you're missing the following line to add the UIScrollView as a subview.
[self.view addSubView:scrollView];

How To Move UIImageView?

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/

Resources