how to pushViewController to another view with ima

2019-06-14 12:24发布

问题:

I have a scrollview of images, I will like to tab them and will pushed to another view.

once i tab on the image, the whole view should push to another view.

From this View to another view

Detail view

Sorry for not asking the question clearly.

my scrollview

-(void)pictureScrolling
{
//init scrollview in location on screen
scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 100, 320, 290)];   
scrollview.backgroundColor = [UIColor redColor];
//pass image filenames
NSMutableArray *fileNames = nearbyFrog.imageFiles; //[[[NSMutableArray alloc] init] autorelease];

//setup the array of uiimageviews
NSMutableArray *imgArray = [[NSMutableArray alloc] init];
UIImageView *imageView;

//loop through the array imgNames to add file names to imgArray
for (NSString *imageName in fileNames) {

    imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:imageName];
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    [imgArray addObject:imageView];
    [imageView release];
}

CGSize pageSize = scrollview.frame.size; 
NSUInteger page = 0;

for (UIView *viewForScrollView in imgArray) {

    [scrollview addSubview:viewForScrollView];

    viewForScrollView.frame = CGRectMake(pageSize.width * page++ +10, 0, pageSize.width -20 , pageSize.height);

    // making use of the scrollView's frame size (pageSize) so we need to;
    // +10 to left offset of image pos (1/2 the gap)
    // -20 for UIImageView's width (to leave 10 gap at left and right)
}
//add scroll view to view
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(pageSize.width * [imgArray count], pageSize.height);

//scrollview.contentSize = CGSizeMake(320 *viewcount + 20, 290 );
scrollview.showsHorizontalScrollIndicator =NO;
[scrollview setPagingEnabled:YES];
scrollview.delegate =self;

//paging function for scrollview
CGRect frame = [[UIScreen mainScreen] applicationFrame];
self.pageControl = [[[UIPageControl alloc] initWithFrame:CGRectMake(0, 100, 100, 50)] autorelease];
self.pageControl.center = CGPointMake(frame.size.width/2, frame.size.height-60);
self.pageControl.numberOfPages = [fileNames count];
[self.view addSubview:self.pageControl];    

//handle Touch Even
[pageControl addTarget:self action:@selector(changePage:) forControlEvents:UIControlEventValueChanged];
[imgArray release];

}

anybody knows how to do it or can show me a tutorial?

Thanks

回答1:

I think this is the best way to implement it

  1. Create your own Custom UIImageView class. This is required to store an additional property which will help you identify which image for clicked.
  2. Create a delegate for that class which is called with the single tap event is raised in the UIImageView
  3. Add the Images inside the scrollview using this custom class. The delegate of this class will tell you which image was tapped. You can use this to push a new view controller passing the image details (if necessary).

You can find some sample code in the ThumbImageView class in the scrollviewSuite sample

http://developer.apple.com/library/ios/#samplecode/ScrollViewSuite/Introduction/Intro.html#//apple_ref/doc/uid/DTS40008904-Intro-DontLinkElementID_2