How can i recive Selected Index Page in UIScrollView - iphone

I'm a beginner at iPhone development. I set 95 pages with (320, 480) pixels in scrollView content size.
I want to get the selected index when I scroll in the UIScrollView.
(void)viewDidLoad {
[super viewDidLoad];
NSLog(#"Selected page %d",scrollView.contentOffset.x) ;
scrollView.pagingEnabled = YES;
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"page (%d).png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
NSLog(#"Selected page %d",scrollView.contentOffset.x) ;
[scrollView addSubview:imageView];
[imageView release];
}
[self layoutScrollImages];
}

what do you want to do after getting that selected index number.because you can
check the condition like if sender.tag==5 if you want to use at other place as you have assigned tag to each page or you can implement scrollview with paging.check this
example
https://github.com/ykyuen/ScrollViewWithPaging

Related

Imagescrollview is showing black screen than images

When clicking on gallery button it shows black screen
UIButton *galleryButton = [UIButton buttonWithType:UIButtonTypeCustom];
[galleryButton addTarget:self action:#selector(ScrollView:) forControlEvents:UIControlEventTouchUpInside];
galleryButton.frame = CGRectMake(0, 0, 30, 30);
UIImage *ime = [UIImage imageNamed:#"photos.png"];
[galleryButton setImage:ime forState:UIControlStateNormal];
UIBarButtonItem *gallerybutton = [[UIBarButtonItem alloc] initWithCustomView:galleryButton];
-(void)ScrollView:(id)sender
{
ImageScrollViewController *imagescrollviewcontroller = [[ImageScrollViewController alloc] init];
[self presentViewController:imagescrollviewcontroller animated:YES completion:NULL];
[imagescrollviewcontroller release];
}
- (void)viewDidLoad
{
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
[_imageScrollView setBackgroundColor:[UIColor blackColor]];
[_imageScrollView setCanCancelContentTouches:NO];
_imageScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
_imageScrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
_imageScrollView.scrollEnabled = YES;
_imageScrollView.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i;
[_imageScrollView addSubview:imageView];
//[imageView release];
}
[self layoutScrollImages];
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [_imageScrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[_imageScrollView setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [_imageScrollView bounds].size.height)];
}
Any idea why it is not showing images but black screen. I am following apple sample code for scrolling images. Changes i have made is that they are showing two subview in scrollview. i am showing only one subview.
Thanks for help.

how to add sounds while uiscrollview is scrolling [duplicate]

This question already has answers here:
How to add sound while UIScrollview is scrolling in iphone sdk?
(2 answers)
Closed 3 years ago.
-(void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
CGFloat curXLoc = 0;
for (view in subviews)
{if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
-(void)viewDidLoad
{
self.view.backgroundColor = [UIColor clearColor];
[scrollView1 setBackgroundColor:[UIColor whiteColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES;
scrollView1.scrollEnabled = YES;
scrollView1.pagingEnabled = YES;
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"snap%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;imageView.tag = i;
[scrollView1 addSubview:imageView];
[imageView release];
[self layoutScrollImages];
}
[super viewDidLoad];
}
add UIScrollViewDelegate to your interface and then do your task in
scrollViewWillBeginDragging or `scrollViewDidEndDragging`
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView // called on start of dragging (may require some time and or distance to move)
{
//add your code here
}
now this method will be called whenever you drag the scrollview

UIScrollView the subviews added but can not slide to them

- (void)viewDidLoad
{
MyData *data = [MyData SharedData];
scrollerView.pagingEnabled = YES;
scrollerView.bounces = YES;
int numberOfViews = [data.placePhotos count];
for (int i = 0; i < numberOfViews; i++) {
CGFloat yOrigin = i * self.view.frame.size.width;
UIView *awesomeView = [[UIView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
awesomeView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 0, 300 , 320)];
NSString * photourl = [[data.placePhotos objectAtIndex:i] stringByReplacingOccurrencesOfString:#"150x75" withString:#"440x440"];
[imageView setImageWithURL:[NSURL URLWithString:photourl]];
[awesomeView addSubview:imageView];
[scrollerView addSubview:awesomeView];
awesomeView = nil;
imageView =nil;
}
I added the UIScrollView in the .xib file , when i tried to pull the scroll view from left to right it does not move, i have 4 subviews added but why i can not view them ?
you should set the content size the scrollView
[self. scrollerView setContentSize:CGSizeMake(320, 1000)];
Give your width and heigth.
add scrollerView.contentSize = CGSizeMake(yOrigin + self.view.frame.size.width, scrollerView.frame.size.height); after the loop (even though I'm a little bit confused about why the x-origin is called yOrigin in your code...)

Scrolling - creating thumbnails from images

Could somebody go over how to modify the Scrolling Project so that it shows one image with others partly showing to the left and right? Just like mobile safari when you go to the page manager...This is key so that the user knows there are more images if they scroll to the right. Both methods taken from Scrolling project.
METHOD TO SET UP SCROLLVIEW:
ViewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
// 1. setup the scrollview for multiple images and add it to the view controller
//
// note: the following can be done in Interface Builder, but we show this in code for clarity
[scrollView1 setBackgroundColor:[UIColor blackColor]];
[scrollView1 setCanCancelContentTouches:NO];
scrollView1.indicatorStyle = UIScrollViewIndicatorStyleWhite;
scrollView1.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
scrollView1.scrollEnabled = YES;
// pagingEnabled property default is NO, if set the scroller will stop or snap at each photo
// if you want free-flowing scroll, don't set this property.
scrollView1.pagingEnabled = YES;
// load all the images from our bundle and add them to the scroll view
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView];
[imageView release];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
}
METHOD TO SET LAYOUT:
- (void)layoutScrollImages
{
UIImageView *view = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIImageView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
in my previous app i create image galley view where i put some custom button on scroll view, so user can see full image on click on that button.
here is code hope it will help you
UIScrollView *Sview = [[UIScrollView alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
int row = 0;
int column = 0;
for(int i = 0; i < self.data.getCustomers.count; ++i) {
customerToEdit = [self.data.getCustomers objectAtIndex:i];
NSURL* imgUrl = [NSURL URLWithString:#"your image url array objects"];
UIImage* thumb = [UIImage imageWithData:[NSData dataWithContentsOfURL:imgUrl]];
//UIImage *thumb = [_thumbs objectAtIndex:i];
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(column*100+24, row*80+10, 64, 64);
[button setImage:thumb forState:UIControlStateNormal];
[button addTarget:self
action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[Sview addSubview:button];
if (column == 2) {
column = 0;
row++;
} else {
column++;
}
}
[view setContentSize:CGSizeMake(320, (row+1) * 80 + 10)];
self.view = view; // or u can [self.view addsubview:Sview];

How to change the image on UIScrollView

I have a scroll view with multiple images. The images are scrollable.
I am using the following code:
scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.pagingEnabled = YES;
scrollView.delegate = self;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.showsHorizontalScrollIndicator = NO;
NSInteger numberOfViews = [arrayImage count];
for (int i = 0; i < numberOfViews; i++)
{
CGFloat yOrigin = i * scrollView.frame.size.width;
imageView = [[UIImageView alloc]initWithFrame:CGRectMake(yOrigin +30, 0, self.view.frame.size.width-60 , self.view.frame.size.height)];
imageView.image = [UIImage imageNamed:[arrayImage objectAtIndex:i]];
[scrollView addSubview:imageView];
[imageView release];
}
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width * numberOfViews,0);
[self.view addSubview:scrollView];
[scrollView release]
The application will work with iPad orientations, but the images are not properly scrolled in the landscape mode.
It works fine with portrait mode, but the images merge in landscape mode.
If somebody has any idea, please share....
Try this one...
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
int scrollWidth = 120;
scrollView.contentSize = CGSizeMake(scrollWidth,80);
int xOffset = 0;
imageView.image = [UIImage imageNamed:[imagesName objectAtIndex:0]];
for(int index=0; index < [imagesName count]; index++)
{
UIImageView *img = [[UIImageView alloc] init];
img.bounds = CGRectMake(10, 10, 50, 50);
img.frame = CGRectMake(5+xOffset, 0, 50, 50);
NSLog(#"image: %#",[imagesName objectAtIndex:index]);
img.image = [UIImage imageNamed:[imagesName objectAtIndex:index]];
[images insertObject:img atIndex:index];
scrollView.contentSize = CGSizeMake(scrollWidth+xOffset,110);
[scrollView addSubview:[images objectAtIndex:index]];
xOffset += 70;
}
Set this...
imagesName = [[NSArray alloc]initWithObjects:#"image1.jpg",#"image2.jpg",#"image3.jpg",#"image4.jpg",#"image5.jpg",#"image6.png",#"image7.png",#"image9.png",nil];
images = [[NSMutableArray alloc]init];
You have to set the image frame for that purpose everytime the interface orientation changes or you have to redraw the images again after the change in orientation