How can i provide Zoomin-Zoomout,Paging functionality in to UIScrollView? - iphone

I have an array of Image URl's from that array i have displayed my Images in Scrollview because i want to take the advantage of Paging and zooming so what should i do to achieve this task?suppose my array contains 10 URL then by each time scrolling i should get different image and i could also performing Zooming-In and Zooming-Out any of the 10 Images so please give me some guidelines to achieve my task.Thanks in Advance.

Try this
- (void)setCurrentPage:(NSUInteger)page {
if (page == offsetX)
return;
offsetX = page;
// in a real app, this would be a good place to instantiate more view controllers -- see SDK examples
}
- (CGSize)pageSize {
CGSize pageSize = scrollView1.frame.size;
if (UIInterfaceOrientationIsLandscape(self.interfaceOrientation))
return CGSizeMake(pageSize.height, pageSize.width);
else
return pageSize;
}
- (void)setPagingMode {
// reposition pages side by side, add them back to the view
CGSize pageSize = [self pageSize];
NSUInteger page = 0;
for (UIView *view in imgViewArray) {
if (!view.superview)
[scrollView1 addSubview:view];
view.frame = CGRectMake(pageSize.width * page++, 0, pageSize.width, pageSize.height);
}
scrollView1.pagingEnabled = YES;
scrollView1.showsVerticalScrollIndicator = scrollView1.showsHorizontalScrollIndicator = NO;
scrollView1.contentSize = CGSizeMake(pageSize.width * [imgViewArray count], pageSize.height);
scrollView1.contentOffset = CGPointMake(pageSize.width * offsetX, 0);
scrollViewMode = ScrollViewModePaging;
}
- (void)setZoomingMode {
NSLog(#"setZoomingMode");
scrollViewMode = ScrollViewModeZooming; // has to be set early, or else currentPage will be mistakenly reset by scrollViewDidScroll
// hide all pages except the current one
NSUInteger page = 0;
for (UIView *view in imgViewArray)
if (offsetX != page++)
[view removeFromSuperview];
scrollView1.pagingEnabled = NO;
scrollView1.showsVerticalScrollIndicator = scrollView1.showsHorizontalScrollIndicator = YES;
pendingOffsetDelta = scrollView1.contentOffset.x;
scrollView1.bouncesZoom = YES;
}
- (void)scrollViewDidScroll:(UIScrollView *)aScrollView
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// offsetX=(aScrollView.contentOffset.x)/768;
//NSLog(#"Page No:%d",offsetX);
if (scrollViewMode == ScrollViewModePaging)
[self setCurrentPage:roundf(scrollView1.contentOffset.x / [self pageSize].width)];
}
else
{
//offsetX=(aScrollView.contentOffset.x)/320;
//NSLog(#"Page No:%d",offsetX);
if (scrollViewMode == ScrollViewModePaging)
[self setCurrentPage:roundf(scrollView1.contentOffset.x / [self pageSize].width)];
}
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)aScrollView
{
NSLog(#"viewForZoomingInScrollView");
if (scrollViewMode != ScrollViewModeZooming)
[self setZoomingMode];
return [imgViewArray objectAtIndex:offsetX];
}
- (void)scrollViewDidEndZooming:(UIScrollView *)aScrollView withView:(UIView *)view atScale:(float)scale {
NSLog(#"scrollViewDidEndZooming");
if (scrollView1.zoomScale == scrollView1.minimumZoomScale)
[self setPagingMode];
else if (pendingOffsetDelta > 0) {
UIView *view = [imgViewArray objectAtIndex:offsetX];
view.center = CGPointMake(view.center.x - pendingOffsetDelta, view.center.y);
CGSize pageSize = [self pageSize];
scrollView1.contentOffset = CGPointMake(scrollView1.contentOffset.x - pendingOffsetDelta, scrollView1.contentOffset.y);
scrollView1.contentSize = CGSizeMake(pageSize.width * scrollView1.zoomScale, pageSize.height * scrollView1.zoomScale);
pendingOffsetDelta = 0;
}
}

Related

how can I change the height of UITableViewRowAction in swift?

I have a cell where the bottom 10px ish is used as a seperator. I want to make the UITableViewRowAction to be the same height as the inner view without the seperator but swift does not seem to support this kind of functionality..
Is there any way to do this?
you can change the size of the Delete button by
-(void)didTransitionToState:(UITableViewCellStateMask)state
{
[super didTransitionToState:state];
if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask)
{
UIView *deleteButton = [self deleteButtonSubview:self];
if (deleteButton)
{
CGRect frame = deleteButton.frame;
frame.origin.y = 4;
frame.size.height = frame.size.height-8;
/*
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
//use your desired size for iPad
frame.size.height = 62;
frame.size.width = 80;
}
else
{
//use your desired size for iPhone
frame.size.height = 52;
frame.size.width = 80;
}
*/
deleteButton.frame = frame;
}
}
}
- (UIView *)deleteButtonSubview:(UIView *)view
{
if ([NSStringFromClass([view class]) rangeOfString:#"Delete"].location != NSNotFound) {
return view;
}
for (UIView *subview in view.subviews) {
UIView *deleteButton = [self deleteButtonSubview:subview];
[deleteButton setBackgroundColor:[UIColor whiteColor]];
if (deleteButton) {
return deleteButton;
}
}
return nil;
}

How do I change the current page for Ray Wenderlich's UIScrollView Tutorial?

I am using Ray Wenderlich's (Matt Galloway's) tutorial on UIScrollViews:
http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
I am using the section, "Paging with UIScrollView" in my app. This works great when you always want the page to load on 0, but I would like the current page to be dynamic. I will be passing this information in based on which thumbnail the user picked from the previous view controller, which is an NSInteger called "listIndex."
How do I change the current page to my listIndex? I have tried changing
self.pageControl.currentPage = 0;
to
self.pageControl.currentPage = self.listIndex;
in viewDidLoad, but this doesn't seem to help - possibly because it's getting reset in loadVisiblePages?
Thank you so much for the help! Here's the code:
- (void)loadVisiblePages {
// First, determine which page is currently visible
CGFloat pageWidth = self.scrollView.frame.size.width;
NSInteger page = (NSInteger)floor((self.scrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));
// Update the page control
self.pageControl.currentPage = page;
// Work out which pages you want to load
NSInteger firstPage = page - 1;
NSInteger lastPage = page + 1;
// Purge anything before the first page
for (NSInteger i=0; i<firstPage; i++) {
[self purgePage:i];
}
// Load pages in our range
for (NSInteger i=firstPage; i<=lastPage; i++) {
[self loadPage:i];
}
// Purge anything after the last page
for (NSInteger i=lastPage+1; i<self.pageImages.count; i++) {
[self purgePage:i];
}
}
- (void)purgePage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// Remove a page from the scroll view and reset the container array
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView != [NSNull null]) {
[pageView removeFromSuperview];
[self.pageViews replaceObjectAtIndex:page withObject:[NSNull null]];
}
}
- (void)loadPage:(NSInteger)page {
if (page < 0 || page >= self.pageImages.count) {
// If it's outside the range of what you have to display, then do nothing
return;
}
// 1
UIView *pageView = [self.pageViews objectAtIndex:page];
if ((NSNull*)pageView == [NSNull null]) {
// 2
CGRect frame = self.scrollView.bounds;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0.0f;
// 3
UIImageView *newPageView = [[UIImageView alloc] initWithImage:[self.pageImages objectAtIndex:page]];
newPageView.contentMode = UIViewContentModeScaleAspectFit;
newPageView.frame = frame;
[self.scrollView addSubview:newPageView];
// 4
[self.pageViews replaceObjectAtIndex:page withObject:newPageView];
}
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
// Load the pages that are now on screen
[self loadVisiblePages];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableString *myHTML = [[NSMutableString alloc] initWithString:#"<html><head><style type='text/css'> p {font-family:sans-serif; text-shadow: 3px 4px 5px #000; color:#FFF; position: absolute; bottom:0; font-size:10pt; font-weight:bold; margin-bottom:5px;}</style><body><p>"];
[myHTML appendString:self.caption];
[myHTML appendString:#"</p></body></html>"];
[captionWebView loadHTMLString:myHTML baseURL:nil];
self.pageImages = [NSMutableArray array];
for (int i=0; i < [imageList count]; i++) {
NSString *path = [(Image *)[imageList objectAtIndex:i] path];
[self.pageImages addObject:[UIImage imageNamed:path]];
}
NSInteger pageCount = self.pageImages.count;
// 2
self.pageControl.currentPage = 0;
//self.pageControl.currentPage = self.listIndex; //Tried this
self.pageControl.numberOfPages = pageCount;
// 3
self.pageViews = [[NSMutableArray alloc] init];
for (NSInteger i = 0; i < pageCount; ++i) {
[self.pageViews addObject:[NSNull null]];
}
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// add this in order UIScrollView work with Auto layout
if (IS_WIDESCREEN) {
self.scrollView.frame = CGRectMake(0, 0, 320, 468);
} else {
self.scrollView.frame = CGRectMake(0, 0, 320, 380);
}
// 4
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// 5
[self loadVisiblePages];
}
- (void)viewWillDisappear:(BOOL)animated {
// add this in order UIScrollView work with Auto layout
if (IS_WIDESCREEN) {
self.scrollView.contentSize = CGSizeMake(320, 468);
} else {
self.scrollView.contentSize = CGSizeMake(320, 380);
}
}
Try setting the scrollview's initial contentOffset in viewWillAppear::
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set up the content size of the scroll view
CGSize pagesScrollViewSize = self.scrollView.frame.size;
self.scrollView.contentSize = CGSizeMake(pagesScrollViewSize.width * self.pageImages.count, pagesScrollViewSize.height);
// Set initial content offset of scrollview
self.scrollView.contentOffset = CGPointMake(pagesScrollViewSize.width * self.listIndex, self.scrollView.contentOffset.y);
// Load the initial set of pages that are on screen
[self loadVisiblePages];
}

dynamic UIScrollView with paging and data from NSMutableArray

I saw alot of people asking about it, I read everything but still could not figure how to do it. I have a UIScrollView with paging enabled, also I have a NSMutableArray with strings.
what I want to do is that the UIScrollView will show to UITextView with the string and when I change to the next page, it will show the next item on the NSMutableArray.
for some reason I can't get it to work.
- (void)setupPage
{
pagingView.delegate = self;
[self.pagingView setBackgroundColor:[UIColor blackColor]];
[pagingView setCanCancelContentTouches:NO];
pagingView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
pagingView.clipsToBounds = YES;
pagingView.scrollEnabled = YES;
pagingView.pagingEnabled = YES;
pagingView.backgroundColor = [UIColor clearColor];
[pagingView setShowsHorizontalScrollIndicator:NO];
[pagingView setShowsVerticalScrollIndicator:NO];
for (int i=0;i<[arrCat count];i++)
{
UITextView * txtJoke = [[UITextView alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
[pagingView addSubview:txtJoke];
txtJoke.text = [arrCat objectAtIndex:i];
txtJoke.textAlignment = UITextAlignmentRight;
}
[pagingView setContentSize:CGSizeMake(960, [pagingView bounds].size.height)];
}
- (void)scrollViewDidScroll:(UIScrollView *)_scrollView
{
if (pageControlIsChangingPage) {
return;
}
/*
* We switch page at 50% across
*/
CGFloat pageWidth = _scrollView.frame.size.width;
int page = floor((_scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
//pageControl.currentPage = page;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
pageControlIsChangingPage = NO;
}
- (IBAction)changePage:(id)sender :(int)current
{
/*
* Change the scroll view
*/
CGRect frame = pagingView.frame;
frame.origin.x = frame.size.width * 1;
frame.origin.y = 0;
[pagingView scrollRectToVisible:frame animated:YES];
/*
* When the animated scrolling finishings, scrollViewDidEndDecelerating will turn this off
*/
pageControlIsChangingPage = YES;
}
This code is a combination of something from another app that i'm trying to fit into this, but without any success.
Please help. Thanks alot.
For one thing, all of your UITextView's have the same frame coordinates, so they will be on top of each other. Assuming you're scrolling horizontally, you need to offset the UITextView's x origin by the page width.
Are you using a UIPageControl? If not, you can just deleted the methods other than setupPage. Those other methods are just used for changing the UIPageControl, and changing the page using the UIPageControl.
Here is a basic scroll view implementation similar to yours:
static NSUInteger kNumberOfPages = 3;
static NSUInteger kPageWidth = 320;
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.pagingEnabled = YES;
self.scrollView.contentSize = CGSizeMake(kPageWidth * kNumberOfPages, self.scrollView.frame.size.height);
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.delegate = self;
self.pageOne.frame = CGRectMake(0, 0, kPageWidth, self.pageOne.frame.size.height);
[self.scrollView addSubview:self.pageOne];
self.pageTwo.frame = CGRectMake(kPageWidth, 0, kPageWidth, self.pageTwo.frame.size.height);
[self.scrollView addSubview:self.pageTwo];
self.pageThree.frame = CGRectMake(2*kPageWidth, 0, kPageWidth, self.pageThree.frame.size.height);
[self.scrollView addSubview:self.pageThree];
self.pageControl.numberOfPages = kNumberOfPages;
self.pageControl.currentPage = 0;
}
# pragma mark - Scroll View Related Methods
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
// We don't want a "feedback loop" between the UIPageControl and the scroll delegate in
// which a scroll event generated from the user hitting the page control triggers updates from
// the delegate method. We use a boolean to disable the delegate logic when the page control is used.
if (pageControlUsed_)
{
// do nothing - the scroll was initiated from the page control, not the user dragging
return;
}
// Switch the indicator when more than 50% of the previous/next page is visible
int page = floor((self.scrollView.contentOffset.x - kPageWidth / 2) / kPageWidth) + 1;
self.pageControl.currentPage = page;
}
// At the begin of scroll dragging, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
pageControlUsed_ = NO;
}
// At the end of scroll animation, reset the boolean used when scrolls originate from the UIPageControl
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed_ = NO;
}
- (IBAction)changePage:(id)sender
{
int page = self.pageControl.currentPage;
// update the scroll view to the appropriate page
CGRect frame = self.scrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
[self.scrollView scrollRectToVisible:frame animated:YES];
// Set the boolean used when scrolls originate from the UIPageControl. See scrollViewDidScroll: above.
pageControlUsed_ = YES;
}

Change Page Control With Button

I have a simple page control set up with two different pages. I have a button called nextButton, and if the user hits that button, I want to change the page and scroll to the next page.
My page control works, and I can scroll to change pages, but the button does not work at all. It is getting called though.
- (void)viewDidLoad
{
[super viewDidLoad];
self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * TUTORIALNUMBEROFPAGES, self.scrollView.frame.size.height);
self.scrollView.pagingEnabled = YES;
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
self.scrollView.scrollsToTop = NO;
self.scrollView.delegate = self;
self.pageControl.numberOfPages = TUTORIALNUMBEROFPAGES;
self.pageControl.currentPage = 0;
CGRect pageFrame = self.scrollView.frame;
pageFrame.origin.x = pageFrame.size.width * 0;
pageFrame.origin.y = 0;
self.pageOne.frame = pageFrame;
[self.scrollView addSubview:self.pageOne];
pageFrame.origin.x = pageFrame.size.width * 1;
self.pageTwo.frame = pageFrame;
[self.scrollView addSubview:self.pageTwo];
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
if (pageControlUsed) {
return;
}
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
self.pageControl.currentPage = page;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
pageControlUsed = NO;
}
- (IBAction)changePage:(id)sender
{
NSInteger currentPage = self.pageControl.currentPage;
CGPoint offset = CGPointMake(currentPage * self.scrollView.frame.size.width, 0);
[self.scrollView setContentOffset:offset animated:YES];
pageControlUsed = YES;
}
- (IBAction)nextButton:(id)sender
{
[self changePage:self];
}
Try something like this:
- (IBAction)nextPage:(id)sender
{        
    NSInteger currentPage = self.pageControl.currentPage + 1;
CGPoint offset = CGPointMake(currentPage * self.scrollView.frame.size.width, 0);
[self.scrollView setContentOffset:offset animated:YES];
pageControlUsed = YES;
}
- (IBAction)nextButton:(id)sender
{
  [self nextPage:self];
}
Notice the +1 in that code. Before, you were literally setting the content offset exactley equal to the original offset because you were just taking the value of the page control.
You can try with this:
- (IBAction)changePage:(id)sender
{
NSInteger currentPage = self.pageControl.currentPage;
if([sender isKindOfClass:[UIButton class]){
currentPage+=1;
}
CGPoint offset = CGPointMake(currentPage * self.scrollView.frame.size.width, 0);
[self.scrollView setContentOffset:offset animated:YES];
pageControlUsed = YES;
}
- (IBAction)nextButton:(id)sender
{
[self changePage:sender];//if this action is attached just to your button
}

program received signal exc_bad_access at scrollViewDidScroll

In my app I tried to do a scrollView with a pageController. The pageController is working but the scrollView is not working. When I try to go to next page I always get program received signal exc_bad_access at line [self loadScrollViewWithPage:page];
from method scrollViewDidScroll
I can't see where is my mystake. Please look at this code...
- (void)viewDidLoad
{
[super viewDidLoad];
[self getVehicules];
vosvehiculeScrollView.pagingEnabled = YES;
vosvehiculeScrollView.contentSize = CGSizeMake(vosvehiculeScrollView.frame.size.width * pageControlVehiculePossedee.numberOfPages, vosvehiculeScrollView.frame.size.height);
vosvehiculeScrollView.showsHorizontalScrollIndicator = NO;
vosvehiculeScrollView.showsVerticalScrollIndicator = NO;
vosvehiculeScrollView.scrollsToTop = NO;
pageControlVehiculePossedee.numberOfPages=[vehiculesPossede count];
pageControlVehiculePossedee.currentPage=0;
[pageControlVehiculePossedee addTarget:self action:#selector(pageAction:) forControlEvents:UIControlEventValueChanged];
for (int i=0; i<pageControlVehiculePossedee.numberOfPages;i++){
[self loadScrollViewWithPage:i];
}
}
- (void) loadScrollViewWithPage: (int) page {
if (page < 0) return;
if (page >= [vehiculesPossede count]) return;
CGRect frame = vosvehiculeScrollView.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
viewVehicules.frame = frame;
[vosvehiculeScrollView addSubview:viewVehicules];
NSLog(#"%d",page);
}
- (void) scrollViewDidScroll: (UIScrollView *) sender {
if (pageControlUsed) {
return;
}
CGFloat pageWidth = vosvehiculeScrollView.frame.size.width;
int page = floor((vosvehiculeScrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
pageControlVehiculePossedee.currentPage = page;
[self loadScrollViewWithPage:page - 1];
[self loadScrollViewWithPage:page];
[self loadScrollViewWithPage:page + 1];
}
- (void) scrollViewWillBeginDragging: (UIScrollView *) scrollView {
pageControlUsed = NO;
}
- (void) scrollViewDidEndDecelerating: (UIScrollView *) scrollView {
pageControlUsed = NO;
}
-(void)pageAction:(UIPageControl*)control
{
NSLog(#"page changed");
int page = pageControlVehiculePossedee.currentPage;
NSLog(#"page %d", page);
CGRect frame;
frame.origin.x = self.vosvehiculeScrollView.frame.size.width * self.pageControlVehiculePossedee.currentPage;
frame.origin.y = 0;
frame.size = self.vosvehiculeScrollView.frame.size;
[self.vosvehiculeScrollView scrollRectToVisible:frame animated:YES];
pageControlUsed = YES;
}