UIPageControll not properly working in UITableView cell - ios

I am implementing a sliding image demo on UITableView, and also I am using UIPageControl for that. When I slide image on first row, the page control changes properly but when I slide image of second row page control starts from where it was left at first row.
Here is my code
-(void)seePhotosBtnClicked:(UIButton *)sender
{
photoView.hidden = NO;
CGPoint touchPoint = [sender convertPoint:CGPointZero toView:addressTable]; // maintable --> replace your tableview name
NSIndexPath *clickedButtonIndexPath = [addressTable indexPathForRowAtPoint:touchPoint];
NSString *areaToSearch = [[addressInfoArray valueForKey:#"IMAGEPATH"]objectAtIndex:clickedButtonIndexPath.row];
NSArray *urlArray = [areaToSearch componentsSeparatedByString: #","];
for (int i = 0; i<=2; i++)
{
NSURL *imageURL = [NSURL URLWithString:[urlArray objectAtIndex:i]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
CGRect frame;
frame.origin.x =pagedScrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = pagedScrollView.frame.size;
UIImageView *imgView=[[UIImageView alloc]initWithFrame:frame];
imgView.image = image;
[pagedScrollView addSubview:imgView];
}
pagedScrollView.contentSize = CGSizeMake(pagedScrollView.frame.size.width * 3, pagedScrollView.frame.size.height);
[pageControl addTarget:self action:#selector(changePage:) forControlEvents:UIControlEventValueChanged];
}
- (void)changePage:(id)sender
{
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = pagedScrollView.frame.size.width * pageControl.currentPage;
frame.origin.y = 0;
frame.size = pagedScrollView.frame.size;
[pagedScrollView scrollRectToVisible:frame animated:YES];
}

Got it, where you stop scrolling photos add this code.
pageControl.currentPage=0;
pagedScrollView.contentSize = CGSizeMake(0, pagedScrollView.frame.size.height);
Now your scrollview will start with first image and page control start with first tick

Related

How change the page control indicator on image slide inside scrollview using NSTimer

Here is the image slider which works fine. When dragging manually the image changes and indicator also changes but when i add animation only image changes automatically not the indicator.
Instead of animating image i want to scroll image so that indicators change
just like it changes on dragging. Also i want to add timer to it.
#implementation DashboardViewController {
NSArray * animationArray;
}
#synthesize scroller = scroller;
#synthesize pageControl = pageControl;
-
(void) viewDidLoad {
[super viewDidLoad];
scroller.pagingEnabled = YES;
scroller.showsHorizontalScrollIndicator = NO;
scroller.delegate = self;
animationArray = [NSArray arrayWithObjects: [UIImage imageNamed: # "image1.jpg"],
[UIImage imageNamed: # "image2.jpg"],
[UIImage imageNamed: # "image3.jpg"],
[UIImage imageNamed: # "image4.png"],
[UIImage imageNamed: # "image5.jpg"],
nil
];
CGRect scrollFrame = CGRectMake(0, 0, self.view.frame.size.width, self.scroller.frame.size.height);
scroller.frame = scrollFrame;
self.scroller.contentSize = CGSizeMake(self.scroller.frame.size.width * animationArray.count, self.scroller.frame.size.height);
for (int i = 0; i < animationArray.count; i++) {
CGRect frame;
frame.origin.x = self.scroller.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
UIImageView * imgView = [
[UIImageView alloc] initWithFrame: CGRectMake(self.scroller.frame.size.width * i, 0, self.scroller.frame.size.width, self.scroller.frame.size.height)
];
imgView.image = [animationArray objectAtIndex: i];
imgView.frame = frame;
imgView.animationImages = animationArray;
imgView.animationDuration = 8;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
[self.scroller addSubview:imgView];
}
self.pageControl.currentPage = 0;
}
-
(void) scrollViewDidScroll: (UIScrollView * ) sender {
if (!pageControlBeingUsed) {
// Switch the indicator when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scroller.frame.size.width;
int page = floor((self.scroller.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
} -
(void) scrollViewWillBeginDragging: (UIScrollView * ) scrollView {
pageControlBeingUsed = NO;
}
-
(void) scrollViewDidEndDecelerating: (UIScrollView * ) scrollView {
[self setIndiactorForCurrentPage];
} -
(void) setIndiactorForCurrentPage {
uint page = scroller.contentOffset.x / scroller.frame.size.width;
[pageControl setCurrentPage: page];
}
-
(IBAction) changePage {
// Update the scroll view to the appropriate page
CGRect frame;
pageControl.currentPage = animationArray.count;
frame.origin.x = self.scroller.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
[self.scroller scrollRectToVisible: frame animated: YES];
pageControlBeingUsed = YES;
}
-
(void) didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
self.scroller = nil;
self.pageControl = nil;
}
Instead of animating image i want to scroll image so that indicators change
just like it changes on dragging. Also i want to add timer to it.
I think you have to set your current page inside your for loop for automatic change pagecontrol with image.
//move this up
self.pageControl.currentPage = 0;
for (int i = 0; i < animationArray.count; i++) {
CGRect frame;
frame.origin.x = self.scroller.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scroller.frame.size;
UIImageView * imgView = [
[UIImageView alloc] initWithFrame: CGRectMake(self.scroller.frame.size.width * i, 0, self.scroller.frame.size.width, self.scroller.frame.size.height)
];
imgView.image = [animationArray objectAtIndex: i];
imgView.frame = frame;
imgView.animationImages = animationArray;
imgView.animationDuration = 8;
imgView.animationRepeatCount = 0;
[imgView startAnimating];
self.pageControl.currentPage = i; //set i to current page
[self.scroller addSubview:imgView];
}

UIScrollView + PageControl

I have a view controller with outlets of scrollview (initial size: 390, 170) and page control. I need it to display an image per page from my array. But i have unexpected result (see image below).
Images are not full-width and after 2nd page scrollView ends.
Here is code
- (void)viewDidLoad {
[super viewDidLoad];
//Put the names of our image files in our array.
imageArray = [[NSArray alloc] initWithObjects:#"header1.jpg", #"header2.jpg", #"header3.jpg", nil];
[self.articlesPageControll setNumberOfPages:imageArray.count];
for (int i = 0; i < [imageArray count]; i++) {
//We'll create an imageView object in every 'page' of our scrollView.
CGRect frame;
frame.origin.x = 390 * i;
frame.origin.y = 0;
frame.size = self.articlesScrollView.frame.size;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
[imageView setContentMode:UIViewContentModeScaleToFill];
imageView.image = [UIImage imageNamed:[imageArray objectAtIndex:i]];
[self.articlesScrollView addSubview:imageView];
}
//Set the content size of our scrollview according to the total width of our imageView objects.
self.articlesScrollView.contentSize = CGSizeMake(self.articlesScrollView.frame.size.width * [imageArray count], 170);
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.articlesScrollView.frame.size.width;
int page = floor((self.articlesScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.articlesPageControll.currentPage = page;
}
UPD: 3rd page with image actually exists, but it's impossible to scroll to this page
Thanks in advance!
Solution self.articlesScrollView.contentSize = CGSizeMake(390 * [imageArray count], 170);

Image scrollview crashes

I've implemented a scrollview with paging to scroll between some images (graphs) at full page (like the Photo app installed in the iPhone).
I found the code below that use the classical 3 pages solution (I made some small modification for my application) but, even if it "works", the scrolling seems to be slow and often after I've scrolled some images the application crashes.
I'm using Xcode 4.2 with ARC option enabled and testing both on an iPad device.
Images (10 jpg) are 2048x1539 with a mean dimension of 200/250Kb each.
Is there anyone that can help me in finding the cause of the problem ?
Thanks,
Corrado
const int numImages = 10;
const float kPageWidth = 1024.0f;
const float kPageHeight = 768.0f;
- (void)viewDidLoad {
[super viewDidLoad];
scroll.contentSize = CGSizeMake(kPageWidth * numImages, kPageHeight);
imageview1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kPageWidth, kPageHeight)];
imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(kPageWidth, 0, kPageWidth, kPageHeight)];
imageview3 = [[UIImageView alloc] initWithFrame:CGRectMake(kPageWidth * 2, 0, kPageWidth, kPageHeight)];
scroll.contentOffset = CGPointMake(0, 0);
[imageview1 setImage:[UIImage imageNamed:#"grafico_0.jpg"]];
imageview1.contentMode = UIViewContentModeScaleAspectFit;
[imageview1 setTag:1];
imageview2.contentMode = UIViewContentModeScaleAspectFit;
[imageview2 setTag:2];
imageview3.contentMode = UIViewContentModeScaleAspectFit;
[imageview3 setTag:3];
[scroll addSubview:imageview1];
[scroll addSubview:imageview2];
[scroll addSubview:imageview3];
}
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
const CGFloat currPos = scrollView.contentOffset.x;
const NSInteger selectedPage = lroundf(currPos * (1.0f / kPageWidth));
const NSInteger zone = 1 + (selectedPage % 3);
const NSInteger nextPage = selectedPage + 1;
const NSInteger prevPage = selectedPage - 1;
/// Next page
if (nextPage < numImages)
{
NSInteger nextViewTag = zone + 1;
if (nextViewTag == 4)
nextViewTag = 1;
UIImageView* nextView = (UIImageView*)[scrollView viewWithTag:nextViewTag];
nextView.frame = (CGRect){.origin.x = nextPage * kPageHeight, .origin.y = 0.0f, kPageHeight, kPageWidth};
NSString *str = [NSString stringWithFormat:#"grafico_%d.jpg", nextPage];
UIImage* img = [UIImage imageNamed:str];
nextView.image = img;
}
/// Prev page
if (prevPage >= 0)
{
NSInteger prevViewTag = zone - 1;
if (!prevViewTag)
prevViewTag = 3;
UIImageView* prevView = (UIImageView*)[scrollView viewWithTag:prevViewTag];
prevView.frame = (CGRect){.origin.x = prevPage * kPageHeight, .origin.y = 0.0f, kPageHeight, kPageWidth};
NSString *str = [NSString stringWithFormat:#"grafico_%d.jpg", prevPage];
UIImage* img = [UIImage imageNamed:str];
prevView.image = img;
}
}
You should not use imageNamed: for the loading of your large images, because that method caches the images and should only be used for small images that you use multiple times in your App (like images for buttons etc.). That method is notorious for causing memory problems when used with many large images.
Switch to imageWithContentsOfFile: instead. Loading your images with that methods secures that the images are not cached and the memory is freed after you do not use that images any more.
If the scrolling seems to be sluggish you can move the loading of the image to a background thread using performSelectorInBackground:
[self performSelectorInBackground:#selector(retrieveImageData:) withObject:imagePath];
the loading of the UIImage happens in this method:
- (void)retrieveImageData:(NSString *)imagePath {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
[self performSelectorOnMainThread:#selector(imageDataRetrieved:) withObject:image waitUntilDone:NO];
[pool release];
}
and the attachment of the image to the UIImageView on the main thread (UI manipulations must not happen on a background Thread):
- (void)imageDataRetrieved:(UIImage)*image {
yourImageView.image = image;
}

How to add multiple UIImageViews to paging UIScrollView?

I have a UIScrollView with paging enabled with multiple subviews on the page: a UIButton, UIwebview and UIImageView. Both the webview and the image change on every page. This works fine. I used Apples scrolling image paging example to get me started.
But when I add a second UIImageView, the position of image I have in place already gets the new values and the new image doesn't display.
This is the code inside viewdidload for the first image (works fine):
// load all the images from our bundle and add them to the scroll view
for (i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:#"image%d.jpg", i];
UIImage *image2 = [UIImage imageNamed:imageName];
UIImageView *imageView2 = [[UIImageView alloc] initWithImage:image2];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView2.frame;
rect.size.height = imageviewScrollObjHeight;
rect.size.width = imageviewScrollObjWidth;
// Get the Layer of any view
CALayer * imageLayer = [imageView2 layer];
[imageLayer setMasksToBounds:YES];
[imageLayer setCornerRadius:7.0];
// You can even add a border
[imageLayer setBorderWidth:1.0];
[imageLayer setBorderColor:[[UIColor lightGrayColor] CGColor]];
imageView2.frame = rect;
imageView2.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageView2];
}
[self layoutScrollImages]; // now place the photos in serial layout within the scrollview
This is the code to layout the first image on every page, different image per page(outside viewdidload)(works fine):
// layout images for imageview1
- (void)layoutScrollImages
{
UIImageView *imageView = nil;
NSArray *subviews = [scrollView1 subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 10;
for (imageView in subviews)
{
if ([imageView isKindOfClass:[UIImageView class]] && imageView.tag > 0)
{
CGRect frame = imageView.frame;
frame.origin = CGPointMake(curXLoc, 50);
imageView.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
// set the content size so it can be scrollable
[scrollView1 setContentSize:CGSizeMake((kNumImages * kScrollObjWidth), [scrollView1 bounds].size.height)];
}
This is the code for the second image (inside viewdidload): (when i remove [self layoutNavScrollImages]; the image loads only on the first page)
for (i = 1; i <= kNumImages; i++)
{
UIImage *navBarImage = [UIImage imageNamed:#"navigationbar.png"];
UIImageView *imageViewNavBar = [[UIImageView alloc] initWithImage:navBarImage];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect navBarRect = imageViewNavBar.frame;
navBarRect.size.height = 44;
navBarRect.size.width = 320;
navBarRect.origin.x = 0;
navBarRect.origin.y = 0;
/* Get the Layer of any view
CALayer * imageLayer = [imageView3 layer];
[imageLayer setMasksToBounds:YES];
[imageLayer setCornerRadius:7.0];
// You can even add a border
[imageLayer setBorderWidth:1.0];
[imageLayer setBorderColor:[[UIColor lightGrayColor] CGColor]];
*/
imageViewNavBar.frame = navBarRect;
imageViewNavBar.tag = i; // tag our images for later use when we place them in serial fashion
[scrollView1 addSubview:imageViewNavBar];
}
[self layoutNavScrollImages];
And the code outside viewdidload:(this overwrites the position of the first image)
- (void)layoutNavScrollImages
{
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)];
}
The way to do this is to make one (big) UIView and add every UIImageView as a subview of that UIView. And then add the UIView as a subview to UIScrollView.
[totalView addSubview:imageView1];
[totalView addSubview:imageView2;
[totalView addSubview:buttonView1];
[totalView addSubview:buttonView2];
[totalView addSubview:webView];
[scrollView addSubview:totalView];
Be sure to set the right content size or scrollview wont work:
[scrollView setContentSize:CGSizeMake((320*kNumImages), 411)];
Setup views and tag the UIView (inside viewdidload):
NSUInteger i;
for (i = 1; i <= kNumImages; i++)
{
//... code to setup views
totalView.tag = i;
}
[self layoutViews]; // now place the views in serial layout within the scrollview
Then to layout the view on every page:
- (void)layoutViews
{
UIView *view = nil;
NSArray *subviews = [scrollView subviews];
// reposition all image subviews in a horizontal serial fashion
CGFloat curXLoc = 0;
for (view in subviews)
{
if ([view isKindOfClass:[UIView class]] && view.tag > 0)
{
CGRect frame = view.frame;
frame.origin = CGPointMake(curXLoc, 0);
view.frame = frame;
curXLoc += (kScrollObjWidth);
}
}
}
I hope this is as clear as possible. If anyone thinks this is not the right way to do it please comment.

UIScrollView and UIPinchGestureRecognizer

I have a UIView with where I load images into subviews with scrolling and paging enabled, it's working ok. Now I'm adding a pinch gesture to zoom on the images and it's working but with a few problems:
I seem to be zooming the whole scrollview, not only the image
I want to limit the zoom out to the orignal size of the image, now I can zoom out way to much and it makes it really small and then it's hard to zoom back.
When I zoom in I can't scroll.
Here's the code:
NSDictionary *dictionary = [tmpAppDelegate.data objectForKey:selectedTitle];
NSArray *MenuImageArr = [[NSArray alloc] init];
MenuImageArr = [dictionary objectForKey:#"MenuArr"];
NSMutableArray *menuImages = [[NSMutableArray alloc] init];
scrollView.delegate = self;
// loop through all names from the plist and add image to the array
for (NSInteger i = 0; i < [MenuImageArr count]; i++) {
NSString *name = [MenuImageArr objectAtIndex:i];
[menuImages addObject:[UIImage imageNamed:[NSString stringWithFormat:#"%#_%#", selectedTitle, name]]];
}
for (int i = 0; i < menuImages.count; i++) {
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * i;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
subview = [[UIImageView alloc] initWithFrame:frame];
subview.image = [menuImages objectAtIndex:i];
[self.scrollView addSubview:subview];
}
self.pageControl.numberOfPages = menuImages.count;
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * menuImages.count, self.scrollView.frame.size.height);
}
- (void)scrollViewDidScroll:(UIScrollView *)sender {
// Update the page when more than 50% of the previous/next page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (IBAction)changePage {
// update the scroll view to the appropriate page
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * self.pageControl.currentPage;
frame.origin.y = 0;
frame.size = self.scrollView.frame.size;
[self.scrollView scrollRectToVisible:frame animated:YES];
pageControlBeingUsed = YES;
}
- (IBAction)handlePinch:(UIPinchGestureRecognizer *)recognizer {
recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
recognizer.scale = 1;
}
I'm not sure how to proceed with these problems.
I do something similiar. In my case I disable zoomin in the scrollview by setting min and maxzoomscale to 1.
My pinch handler will then just work out the zoom factor from the gestures parameters and tell the view contained in the scrollview to repaint. This will then handle the zooming itself. The pinch handle should constrain the zoom factor. Remember the scale factor passed in from the gesture recognizer is not an absolute scale but a delta from the last event.
You need to do something like this:
...
float lastScale = 1;
...
- (void) pinchGestureRecognizer:(UIPinchGestureRecognizer*) recognizer
{
float newScale = self.myScale + ( recognizer.scale - lastScale )
self.myDrawing.scale = newScale;
if ( recognizer.state === UIGestureRecognizerStateEnded )
{
lastScale = 1;
return;
}
lastScale = recognizer.scale;
[self.myDrawing setNeedsDisplay];
}
Thats off the top of my head so hopefully will help
My exact situation is slightly different as I'm manually drawing some SVG vectors but the principles should be pretty similiar
Solved! Using a UIWebview loading a pdf. Had some trouble getting the correct path in main bundle in pathForResource.
NSString *path = [[NSBundle mainBundle] pathForResource:#"Millennium_meny" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[webView setScalesPageToFit:YES];
Thanx!

Resources