PageView slideshow - ios

I'm new here and could use some help, i'm making an ios app for the ipad and want to be able to animate the transition of pages in a pageview with custom and/or stock transitions.
What is the correct way to animate through various pages (PageView) each having full screen photos in them? Similar to in apples photos app. I'd like to be able to set up a basic slideshow and transition through them.
All Help is appreciated.
Thank You
Edit: Wow those responses were fast, is it also possible to have custom animations?

you can create like this way:-
- (void)viewDidLoad {
[super viewDidLoad];
UIScrollView *scr=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scr.tag = 1;
scr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:scr];
[self setupScrollView:scr];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 264, 480, 36)];
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
}
- (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.
for (int i=1; i<=10; i++) {
// create image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"sti%02i.jpeg",i]];
// create imageView
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*10, scrMain.frame.size.height)];
// enable timer after each 2 seconds for scrolling.
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:#selector(scrollingTimer) userInfo:nil repeats:YES];
}
- (void)scrollingTimer {
// access the scroll view with the tag
UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
// same way, access pagecontroll access
UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
// get the current offset ( which page is being displayed )
CGFloat contentOffset = scrMain.contentOffset.x;
// calculate next page to display
int nextPage = (int)(contentOffset/scrMain.frame.size.width) + 1 ;
// if page is not 10, display it
if( nextPage!=10 ) {
[scrMain scrollRectToVisible:CGRectMake(nextPage*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=nextPage;
// else start sliding form 1 :)
} else {
[scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
pgCtr.currentPage=0;
}
}
You can use:-
SDWebimage
AFNetworking
NSCache and use of NSCache
For image cache and asynchronous load image
UPDATE:-
Here Bellow is some Library that doing something like photo gallery slide show check this and if you want then use in to you project by reading How to Implement.
MWPhotoBrowser for iOS
EGOPhotoViewer for iOS
CXPhotoBrowser for iOS
FGallery for iOS
Nimbus for iOS
PTImageAlbumViewController for iOS

Related

how to create scroll view with images in Horizontal through programmatically?

Hi I am new to iOS how to create scroll view with images programmatically
like this I have tried, I am getting scroll view with images in horizontal but the images are placed like below pic
Am I doing in correct way?
please help me .
Thanks
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// CGRectMake(0, 0, 600, 200);
_scrollView.tag = 1;
_scrollView.autoresizingMask=UIViewAutoresizingNone;
NSLog(#"--->%#",_scrollView);
[self.view addSubview:_scrollView];
[self setupScrollView:_scrollView];
UIPageControl *pgCtr = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 154, 480, 36)];
NSLog(#"--->%#",pgCtr);
[pgCtr setTag:12];
pgCtr.numberOfPages=10;
pgCtr.autoresizingMask=UIViewAutoresizingNone;
[self.view addSubview:pgCtr];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.
for (int i=1; i<=3; i++) {
// create image
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"abc%d.png",i]];
// create imageView
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(((i-1)*scrMain.frame.size.height+5), 0, 320, 195)];
// set scale to fill
_imageView.contentMode=UIViewContentModeScaleToFill;
// set image
[_imageView setImage:image];
// apply tag to access in future
_imageView.tag=i+1;
NSLog(#"--->%#",_imageView);
// add to scrollView
[scrMain addSubview:_imageView];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(320*3, scrMain.frame.size.height)];
NSLog(#"--->%#",scrMain);
// enable timer after each 2 seconds for scrolling.
[NSTimer scheduledTimerWithTimeInterval:7 target:self selector:#selector(scrollingTimer) userInfo:nil repeats:YES];
}
- (void)scrollingTimer {
// access the scroll view with the tag
UIScrollView *scrMain = (UIScrollView*) [self.view viewWithTag:1];
NSLog(#"--->%#",scrMain);
// same way, access pagecontroll access
UIPageControl *pgCtr = (UIPageControl*) [self.view viewWithTag:12];
NSLog(#"--->%#",pgCtr);
// get the current offset ( which page is being displayed )
CGFloat contentOffset = 194;
NSLog(#"------------------->%f",contentOffset);
// calculate next page to display
int nextPage = (int)(contentOffset/scrMain.frame.size.height) + 1 ;
NSLog(#"%d",nextPage);
// if page is not 10, display it
if( nextPage!=10 ) {
[scrMain scrollRectToVisible:CGRectMake(0, nextPage*scrMain.frame.size.height, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
NSLog(#"--->%#",scrMain);
pgCtr.currentPage=nextPage;
// else start sliding form 1 :)
} else {
[scrMain scrollRectToVisible:CGRectMake(0, 0, scrMain.frame.size.width, scrMain.frame.size.height) animated:YES];
NSLog(#"-------------------------------------------->%#",scrMain);
pgCtr.currentPage=0;
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I think, you are wrong at this line:
// create imageView
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(((i-1)*scrMain.frame.size.height+5), 0, 320, 195)];
It should be:
// create imageView // change height to width
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(((i-1)*scrMain.frame.size.width+5), 0, 320, 195)];
Please implement this code.Hope this works and use these macros.
#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height
#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
- (void)setupScrollView:(UIScrollView*)scrMain
{
for (int i=0; i<3; i++)
{
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:#"abc%d.png",i]];
UIImageView *_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i*SCREEN_WIDTH, 0, SCREEN_WIDTH, 195)];
_imageView.contentMode=UIViewContentModeScaleToFill;
[_imageView setImage:image];
_imageView.tag=i;
[scrMain addSubview:_imageView];
}
[scrMain setContentSize:CGSizeMake(SCREEN_WIDTH*3, scrMain.frame.size.height)];
[NSTimer scheduledTimerWithTimeInterval:7 target:self selector:#selector(scrollingTimer) userInfo:nil repeats:YES];
}
You can add custom view to Scrollview and in Custom view add UIImageview as subview with some padding left and right?

ios - Delete or reset memory used when going back to root viewcontroller

Good day! Is there any way on how to reduce or revert the memory used to a freshly opened app state? I'm developing a Menu app for a restaurant. On the main screen i have a buttons for Main Course,Appetizers,Soups etc. If you click on one category it will take you to another view, That is UINavigation Controller with UITableViewController, now when you click on any row of that UITableView it will take you to another ViewController which contains UIScrollView with images. This Last ViewController has this code to loop many images.
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
self.scrollView.pagingEnabled = YES;
[self.scrollView setAlwaysBounceHorizontal:NO];
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.scrollEnabled = YES;
//setup internal views
NSInteger numberOfViews = 5;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image.image = [UIImage imageNamed:[NSString stringWithFormat:
#"main_%d", i+1]];
image.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:image];
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(updatePrices:) userInfo:nil repeats:YES];
}
//set the scroll view content size
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfViews,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:self.scrollView];
So basically in 1 Category Lets say Main Course, i have this
http://oi60.tinypic.com/f1fr0j.jpg
Each category i have that setup of views. Now, The main problem is, when i open the app, it only consumes 6MB, then when i Tap a button, to show the Menu for the selected category, it goes up to 17mb, Now When i click anything on the Food lists, and shows the image viewcontroller, it goes up to 68mb. I tried to add a button on each to and add some actions like this
- (IBAction)backToHome:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
self.view = nil;
}
The main problem is, When i go back to the MainViewController, The consumed Memory is there, so the app gets really slow if they select more categories because it increases the memory usage. How can i Reduce or "Clear Cache" of the used memory to prevent slowing the app? Thank you!
Calling imageNamed will always cache the image in the memory to increase speed, so event if you release holder view(s) the memory usage won't drop. Use some other method to load your images(imageWithContentsOfFile or other) and make caching by yourself if you need it at all.
#property(nonatomic, strong) NSMutableDictionary* cachedImages;
-(UIImage*)imageNamedBOOM:(NSString*)aImageName
{
UIImage* result = _cachedImages[aImageName];
if( !result )
{
result = [UIImage imageWithContentsOfFile:aImageName];
_cachedImages[aImageName] = result;
}
return result;
}
-(void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
[_cachedImages removeAllObjects];
}
And then in your code just call it like this(note that you have to provide image extension png, jpg, etc):
-(void) setupScrollView {
//add the scrollview to the view
self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
self.scrollView.pagingEnabled = YES;
[self.scrollView setAlwaysBounceVertical:NO];
//setup internal views
NSInteger numberOfViews = 3;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
UIImageView *image = [[UIImageView alloc] initWithFrame:
CGRectMake(xOrigin, 0,
self.view.frame.size.width,
self.view.frame.size.height)];
image.image = [self imageNamedBOOM:[NSString stringWithFormat:
#"image_%d.png", i+1]];//make sure here you provide proper image extension JPG, PNG or other
image.contentMode = UIViewContentModeScaleAspectFit;
[self.scrollView addSubview:image];
}
//set the scroll view content size
self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width *
numberOfViews,
self.view.frame.size.height);
//add the scrollview to this view
[self.view addSubview:self.scrollView];
}
And in back to home:
- (IBAction)backToHome:(id)sender {
[_cachedImages removeAllObjects]
[self dismissViewControllerAnimated:YES completion:nil];
self.view = nil;
}

Unable to scroll iCarousel images

I'm using iCarousel library in my small app. I'm getting the image urls via web service and placing those images to the iCarousel library.
First I created a UIView and added iCarousel as respective class to it. Latter set the datasourse and delete gate to same class.
Now everything looks cool and I could see the images but I couldn't swipe the images.
Following is my code.
- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
//return the total number of items in the carousel
return [galleryItems count];
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
UILabel *label = nil;
//create new view if no view is available for recycling
if (view == nil)
{
//NSString *ImageURL = #"YourURLHere";
//NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ImageURL]];
//imageView.image = [UIImage imageWithData:imageData];
view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];
//[((UIImageView *)view) setImageWithURL:[NSURL URLWithString:#"http://www.pizzatower.com/img/icons/Pizza-icon.png"]];
NSURL *imageURL = [[galleryItems objectAtIndex:index] galleryURL];
[((UIImageView *)view) setImageWithURL:imageURL];
view.contentMode = UIViewContentModeScaleAspectFit;
label = [[UILabel alloc] init];
label.backgroundColor = [UIColor clearColor];
label.font = [label.font fontWithSize:20];
label.tag = 1;
// label.contentMode = UIViewContentModeBottom;
// label.frame = CGRectMake(
// self.view.frame.size.width - label.frame.size.width,
// self.view.frame.size.height - label.frame.size.height,
// label.frame.size.width,
// label.frame.size.height );
//label.bounds = view.bounds;
[view addSubview:label];
}
else
{
//get a reference to the label in the recycled view
label = (UILabel *)[view viewWithTag:1];
}
//set item label
//remember to always set any properties of your carousel item
//views outside of the `if (view == nil) {...}` check otherwise
//you'll get weird issues with carousel item content appearing
//in the wrong place in the carousel
//label.text = [[galleryItems objectAtIndex:index] galleryDescription];
return view;
}
I've used same code base and it worked in some other project. No idea what am I missing here. Can someone please enlighten me.
You UIView containing the carousel may have userInteractionEnabled set to NO.
Another possibility is that the UIView containing the carousel may be smaller than the carousel and so be blocking interaction. Try setting its background color to see its true size.
I'd suggest delete your nib or the ViewController in your storyboard and re-create it. I had the same issue once and after re-creating it it worked. Make sure you center your UIView when placing it (both horizontally and vertically)

Display Content on Sections of UIScrollView

I am trying to hide content on only one section (one photo) of my UIScrollView and am using a contentOffsetto access that section like below. If the content offset is between 0 and 320 (the size of my image) I am hiding various elements on the screen. However when the user swipes over to the next photo (i.e. content offset greater than 320) I would like to show the content again. The first portion of my code works great, however I am not sure that there is any detection of the content offset being greater than 320 when the user swipes. I am fairly new with ScrollViews and am not sure if they update the content offset constantly or whether they detect user swipe that would cause an update. Are there any ideas on how I can get the elements hidden to equal NO after the user has scrolled beyond my first image?
if (scroller.contentOffset.x >=0<=320)
{
preview.hidden = YES;
share.hidden = YES;
barView.hidden = YES;
saveButton.hidden = YES;
shaddows.hidden = YES;
}
else if (scroller.contentOffset.x >320)
{
//This part does not work
preview.hidden = NO;
share.hidden = NO;
barView.hidden = YES;
saveButton.hidden = YES;
shaddows.hidden = YES;
}
Here is the rest of my scroller code:
int PageCount = 2;
NSMutableArray *myArray =[[NSMutableArray alloc]initWithObjects:#"12-4.png",#"13-4.png",nil];
scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
scroller.scrollEnabled=YES;
scroller.backgroundColor = [UIColor clearColor];
scroller.pagingEnabled = YES;
[self.view addSubview:scroller];
width=scroller.frame.size.width;
xPos=0;
for (int i=0; i<PageCount; i++)
{
ImgView = [[UIImageView alloc]initWithFrame:CGRectMake(xPos, 0, scroller.frame.size.width, scroller.frame.size.height)];
[ImgView setImage:[UIImage imageNamed:[myArray objectAtIndex:i]]];
[scroller addSubview:ImgView];
scroller.contentSize = CGSizeMake(width, 0);
width +=scroller.frame.size.width;
xPos +=scroller.frame.size.width;
}
Thank you!
Change this line
if (scroller.contentOffset.x >=0<=320)
To
if (scroller.contentOffset.x <= 320)
Because the original doesn't do what you think it will.
This code should be in the scrollViewDidScroll: delegate method (make sure your controller is the scroll view delegate).
Add some log statements so it prints out as the user is scrolling what the content offset it and what page your code thinks it is.

Clearing Buffer Memory in Xcode

I make a app which include a image view within scroll view.
I have 3 classes and each class add images into image view.
When i press button1, it call class1 and it fill Class1images(from image001 to image200).
When i press button2, it call class2 and it fill Class2images(from image201 to image400)
The problem is when i call class1, image view show class1images(from image001 to image200).
After calling class1, i call class2.
When i call class2, my image view can't show class2images(from image201 to image400)
But, class1Images (from image001 to image200) are remaining in app.
I think class1 images are still in memory, so that class2 images can't add to memory.
I want to remove class1images when i call class2.
When i call next class, it will remove old images in memory and replace with new images.
But, my app is develop with storyboard and ARC.
So, i can't release memory manually in ARC mode.
Is there any ways to remove old images in memory?
- (void)viewDidLoad{
[super loadView];
self.view.backgroundColor = [UIColor grayColor];
ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height)];
ScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 200;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:#"Image001.jpg"];
UIImage *image2 = [UIImage imageNamed:#"Image002.jpg"];
:
:
UIImage *image200 = [UIImage imageNamed:#"Image200.jpg"];
NSArray *images = [[NSArray alloc] initWithObjects:image1,image2, . . . ,image200,nil];
ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0,self.view.frame.size.width, self.view.frame.size.height)];
[ImageView setImage:[images objectAtIndex:i]];
[ScrollView addSubview:ImageView];}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width*numberOfViews,self.view.frame.size.height);
[self.view addSubview:ScrollView];}
setting an imageview's animationImages property to a new array will remove the olf array and animate the new one.
to be 110% safe call stopAnimation, set the images, call startAnimation
I think your scrollview is not getting cleaned up. You need to remove previous images from scrollView and after that add images from next class.
try to implement something like this for removing images from scrollview and after this add other images on button click
for(UIView *subView in self.ScrollView.subviews)
{
if([subView isKindOfClass:[UIImageView class]])
[subView removeFromSuperview];
}
Hopefully can help you. Just give it a try.

Resources