Animation in content of ScrollView - ios

I want to animate content of scrollview i have tried this for animation.
- (void)viewDidLoad {
[super viewDidLoad];
//Scroll View Created Programmatically
scrollview=[[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollview.delegate=self;
scrollview.contentSize=CGSizeMake(scrollview.bounds.size.width, self.view.bounds.size.height);
[self.view addSubview:scrollview];
// Road Image Created Programmatically
backgroundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
backgroundImageView.image = [UIImage imageNamed:#"roadds.png"];
backgroundImageView.contentMode = UIViewContentModeRedraw;
[scrollview addSubview:backgroundImageView];
// Tree top left Created Programmatically
Tree = [[UIImageView alloc] initWithFrame: CGRectMake(0, 0, 30, 50)];
Tree.image = [UIImage imageNamed:#"rsz_1rsz_tree.png"];
Tree.contentMode = UIViewContentModeTopLeft;
[scrollview addSubview:Tree];
//Tree top Right
Tree1 = [[UIImageView alloc] initWithFrame: CGRectMake(self.view.frame.size.width-30, 0, 30, 50)];
Tree1.image = [UIImage imageNamed:#"rsz_tree1.png"];
Tree1.contentMode = UIViewContentModeTopRight;
[scrollview addSubview:Tree1];
//Tree Bottom left
Tree2 = [[UIImageView alloc] initWithFrame: CGRectMake(0, self.view.frame.size.height-80, 30, 50)];
Tree2.image = [UIImage imageNamed:#"rsz_tree2.png"];
Tree2.contentMode = UIViewContentModeBottomLeft;
[scrollview addSubview:Tree2];
//Tree Bottom Right
Tree3 = [[UIImageView alloc] initWithFrame: CGRectMake(self.view.frame.size.width-30, self.view.frame.size.height-80, 30, 50)];
Tree3.image = [UIImage imageNamed:#"rsz_tree3.png"];
Tree3.contentMode = UIViewContentModeBottomRight;
[scrollview addSubview:Tree3];
// Car
car = [[UIImageView alloc] initWithFrame: CGRectMake((self.view.frame.size.width)/2+20, self.view.frame.size.height-120, 40, 60)];
car.image = [UIImage imageNamed:#"rsz_car1.png"];
car.contentMode = UIViewContentModeCenter;
[scrollview addSubview:car];
//Grass
grass = [[UIImageView alloc] initWithFrame:CGRectMake(0, (self.view.frame.size.height)/2,30, 30)];
grass.image = [UIImage imageNamed:#"rsz_grass.png"];
grass.contentMode = UIViewContentModeBottomLeft;
[scrollview addSubview:grass];
//Grass
grass1 = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-60, (self.view.frame.size.height)/2,30, 30)];
grass1.image = [UIImage imageNamed:#"rsz_grass1.png"];
grass1.contentMode = UIViewContentModeBottomLeft;
[scrollview addSubview:grass1];
// Left Arrow
left = [[UIButton alloc] initWithFrame:CGRectMake((self.view.frame.size.width)/2+60, (self.view.frame.size.height-35),30, 30)];
[left setBackgroundImage:[UIImage imageNamed:#"rsz_arrowl.png"]forState:UIControlStateNormal];
left.contentMode = UIViewContentModeBottomRight;
[scrollview addSubview:left];
//right arrow
right = [[UIButton alloc] initWithFrame:CGRectMake((self.view.bounds.size.width)/2 - 90, (self.view.frame.size.height-35),30, 30)];
[right setBackgroundImage:[UIImage imageNamed:#"rsz_arrowr.png"]forState:UIControlStateNormal];
right.contentMode = UIViewContentModeBottomLeft;
[scrollview addSubview:right];
// For Animation
CGRect bounds = scrollview.bounds;
CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:#"bounds"];
animation.duration = 10;
animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
animation.fromValue = [NSValue valueWithCGRect:bounds];
bounds.origin.y += 100;
animation.toValue =[NSValue valueWithCGRect:bounds];
[scrollview.layer addAnimation:animation forKey:#"bounds"];
scrollview.bounds = bounds;
}

Add UIView to ScrollView and then add your all stuff to this view.
And add animation to this view or view's layer. Hope this will work. :)
If you want your initial position back after animation add this line
bounds.origin.y -= 100;
after
[scrollview.layer addAnimation:animation forKey:#"bounds"];
so, last five line of your viewdidLoad should as below,
bounds.origin.y += 100;
animation.toValue =[NSValue valueWithCGRect:bounds];
[scrollview.layer addAnimation:animation forKey:#"bounds"];
bounds.origin.y -= 100;
scrollview.bounds = bounds;

Related

In my app , when I scroll , the image is going over the battery and signal bar(as shown in the image) which I don't want . How can I make it happen/

The scroll is covering the time and carrier , I just want it not to overlapp but the image view should finish below the carrier bar .
How can this be done.
This is my code for the same
tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320 , self.view.frame.size.height)];
self.tableView.delegate = self;
self.tableView.dataSource = self;
UIImage *image = [UIImage imageNamed:#"dora.jpeg"];
UIGraphicsBeginImageContext(CGSizeMake(320, 480));
[image drawInRect:CGRectMake(0, 0, 320, 480)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imageView = [[UIImageView alloc] initWithImage:image];
CGRect frame = imageView.frame;
frame.origin.y -= 130;
defaultY = frame.origin.y;
imageView.frame = frame;
self.tableView.backgroundColor = [UIColor clearColor];
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 170)];
header.backgroundColor = [UIColor clearColor];
self.tableView.tableHeaderView = header;
[self.view addSubview:imageView];
[self.view addSubview:self.tableView];
defaultSize = CGSizeMake(50, 20);
scrollPanel = [[UIView alloc] initWithFrame:CGRectMake(-defaultSize.width, 0, defaultSize.width, defaultSize.height)];
scrollPanel.backgroundColor = [UIColor blackColor];
scrollPanel.alpha = 0.45;
Thanks
This issue can be very frustrating, and I believe it is a bug on Apple's end, especially because it shows up in their own pre-wired TableViewController from the object library.
For more info refer
iOS 7: UITableView shows under status bar
Set the origin.y of your scrollView (or table) to height of the status bar:
CGRect scrollControllFrame = scrollControll.frame;
scrollControllFrame.origin.y = [UIApplication sharedApplication].statusBarFrame.size.height;
scrollControllFrame.size.height-=[UIApplication sharedApplication].statusBarFrame.size.height;
scrollControll.frame = scrollControllFrame;

UILongPressGestureRecognizer on a navigationbar's view

I'm using this code to show a round user's avatar on my navigation bar:
UIView *avatView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 190.0f)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(-20, 75, 40, 40)];
imageView.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
imageView.image = [UIImage imageWithData:myUtente.imgData];
imageView.layer.masksToBounds = YES;
imageView.layer.cornerRadius = 20.0;
imageView.layer.borderColor = [UIColor blueColor].CGColor;
imageView.layer.borderWidth = 1.0f;
imageView.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageView.layer.shouldRasterize = YES;
imageView.clipsToBounds = YES;
[avatView addSubview:imageView];
self.navBar.titleView = avatView;
Now I'd like this avatar to act like a button, with a 2 seconds long press on it, so I've added this code:
UILongPressGestureRecognizer *longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:#selector(switchUser:)];
[longPressGesture setMinimumPressDuration:2.0f];
// same code as above
[avatView addGestureRecognizer:longPressGesture];
The corresponding action never gets called and I don't know why. Any idea?
Thanks.
Change the frame of avatView and imageView then gesture works like a charm.
ex:
UIView *avatView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(80, 2, 40, 40)];

How can i add multiple images to my scrollView in ios

i have scroll View with three slides,i want to add images to my scroll view.this is my code after this what do i need to add
- (void)loadView {
[super loadView];
self.view.backgroundColor = [UIColor redColor];
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0.5/i green:0.5 blue:0.5 alpha:1];
[scroll addSubview:awesomeView];
[awesomeView release];
}
scroll.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:scroll];
[scroll release];
}
If you want to add Images horizontally (say 3 images), than you have to add scroll view with the width of 3*yourImageView.frame.size.width and than you can add those Image on (x=0,y=0),
(x = yourImageView.frame.size.width, y=0), and (x = 2*yourImageView.frame.size.width, y=0)
UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, yourImageView.frame.size.width, self.view.frame.size.height)];
scroll.pagingEnabled = YES;
NSInteger numberOfViews = 3;
imageView1.frame = CGrectMake(0,0,imageView1.frame.size.width,imageView1.frame.size.height);
imageView2.frame = CGrectMake(0,imageView1.frame.size.width,imageView2.frame.size.width,imageView2.frame.size.height)
imageView3.frame = CGrectMake(0,2*imageView1.frame.size.width,imageView3.frame.size.width,imageView3.frame.size.height)
[scroll addSubview:imageView1];
[scroll addSubview:imageView2];
[scroll addSubview:imageView3];
enable horizontal scrolling

Showing UIButton Done on UIScrollview for images

Want to show UIButton done on UIScrollView for images. With the below code UIButton is showing but when I scroll UIImageViews, it shows only on very first imageview where as I want UIButton done should show on all image views in UIScrollView.
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor blackColor];
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(0, 0, 60, 35);
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
[imageView release];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
How should I code it that UIButton Done should show on all image views when I scroll it.
Add your UIButton in for loop i.e. it can add each time when your new ImageView is add.
And change its x_Origin for each imageView -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 61;
for (int i = 0; i < numberOfViews; i++)
{
CGFloat xOrigin = i * self.view.frame.size.width;
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:#selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(xOrigin, 0, 60, 35);
[myButton setTitle:#"Done" forState:UIControlStateNormal];
myButton.backgroundColor = [UIColor blackColor];
[myButton.layer setMasksToBounds:YES];
[myButton.layer setCornerRadius:10.0f];
myButton.layer.borderWidth = 2;
myButton.layer.borderColor = [[UIColor whiteColor] CGColor];
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
}
Remove these 2 lines
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myButton];
[[self navigationItem] setRightBarButtonItem:button animated:YES];
There is no need to do this. This means you're setting that button on rightBarButton of NavigationBar but in your case i think you dont want to do so.
Set frame for myButton in for loop similar to imageview frame.
myButton.frame= CGRectMake(xOrigin, 0, self.myButton.frame.size.width, self.myButton.frame.size.height);

wait_fences: failed to receive reply: 10004003 - viewDidLoad

I'm getting this error and curious whats triggering it. From what I've read it's usually about animating inside viewDidLoad or blocking a UIAlertView.
1.If I comment out the scrollView block of code, the error goes away.
2.If I leave the scrollView code and comment out from 360Controller start to page One end. The warning goes away.
3.Is it because of memory overload?
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.hidden = YES;
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor grayColor];
//SCROLLVIEW Start
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 1024, 768)];
[scrollView setContentSize:CGSizeMake(1024*6, 768)];
scrollView.bounces = NO;
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = YES;
scrollView.delegate = self;
[self.view addSubview:scrollView];
//SCROLLVIEW End
//Poster Vertical Scroller STart
posterVerticalScroller = [[UIScrollView alloc] initWithFrame:CGRectMake(1024*3, 0, 1024, 768)];
[posterVerticalScroller setContentSize:CGSizeMake(1024, 768*3)];
posterVerticalScroller.bounces = NO;
posterVerticalScroller.pagingEnabled = YES;
[scrollView addSubview:posterVerticalScroller];
//Poster Vertical Scroller End
//Overview Start
overView = [[OverViewController alloc] initWithNibName:nil bundle:nil];
overView.view.frame = CGRectMake(1024, 0, 1024, 768);
[scrollView addSubview:overView.view];
//OverView End
//Landing start
landingView = [[LandingViewController alloc] initWithNibName:nil bundle:nil];
landingView.view.frame = CGRectMake(0, 0, 1024, 768);
[scrollView addSubview:landingView.view];
//Landing End
//360Controller Start
rotateController * rotateAnim = [[rotateController alloc] initWithNibName:nil bundle:nil];
rotateAnim.view.frame = CGRectMake(1024*2, 0, 1024, 768);
[scrollView addSubview:rotateAnim.view];
animateHouse = [[UIImageView alloc] initWithFrame:CGRectMake(100 + 1024*2, 200, 800, 450)];
// animateHouse.animationImages = rotateImg;
//animateHouse.animationDuration = 3.0;
// animateHouse.animationRepeatCount = 1;
// [animateHouse startAnimating];
animateHouse.userInteractionEnabled = YES;
animateHouse.image = [UIImage imageNamed:#"1.png"];
[self.view addSubview:animateHouse];
//360Controller End
//Poster 1 start
poster1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster01.png"]];
poster1.frame = CGRectMake(0, 0, 1250, 768);
[posterVerticalScroller addSubview:poster1];
poster1Items = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster01_items.png"]];
poster1Items.frame = CGRectMake(0, 0, 1024, 768);
[posterVerticalScroller addSubview:poster1Items];
//Poster 1 end
//Poster 2 Start
poster2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster02.png"]];
poster2.frame = CGRectMake(0, 768, 1162, 768);
[posterVerticalScroller addSubview:poster2];
poster2Items = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster02_items.png"]];
poster2Items.frame = CGRectMake(0, 768, 1024, 768);
[posterVerticalScroller addSubview:poster2Items];
//Poster 2 End
//Poster 3 Start
poster3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster03.png"]];
poster3.frame = CGRectMake(0, 768*2, 1536, 768);
[posterVerticalScroller addSubview:poster3];
poster3Items = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"poster03_items.png"]];
poster3Items.frame = CGRectMake(0, 768*2, 1024,768 );
[posterVerticalScroller addSubview:poster3Items];
//Poster 3 End
//PageOne Start
UIImageView * pageOneBg = [[UIImageView alloc] initWithFrame:CGRectMake(1024*4, 0, 1024, 768)];
pageOneBg.image = [UIImage imageNamed:#"page360_landing_full.png"];
[scrollView addSubview:pageOneBg];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(1024*4+750, 520, 200, 200);
[button setTitle:#"" forState:UIControlStateNormal];
[button addTarget:self action:#selector(loadingTEst) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:button];
//PageOne End
//Page 2
UIImageView *pageTwoBG = [[UIImageView alloc] initWithFrame:CGRectMake(1024*5, 0, 1024, 768)];
pageTwoBG.image = [UIImage imageNamed:#"Page06_Full.png"];
[scrollView addSubview:pageTwoBG];
UIButton *buttonTwo = [UIButton buttonWithType:UIButtonTypeCustom];
buttonTwo.frame = CGRectMake(1024*5+0, 550, 250, 100);
[buttonTwo setTitle:#"" forState:UIControlStateNormal];
[buttonTwo addTarget:self action:#selector(loadMap) forControlEvents:UIControlEventTouchUpInside];
[scrollView addSubview:buttonTwo];
//Page 2 END
}
Try putting your animation code in viewDidAppear. If the animations only need to run when the view is loaded, then use a boolean to check if the view has been reloaded.

Resources