Simple image browser [zoomed images, sliding back to normal]

Source: Internet
Author: User

Appdelegate. h

#import "AppDelegate.h"#import "RootViewController.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];    // Override point for customization after application launch.    self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];        RootViewController *rootCtrl = [[RootViewController alloc] init];        UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:rootCtrl];        NSMutableArray *mutArrary = [[NSMutableArray alloc] init];        for (int i=0; i<5; i++)    {        NSString *imgName = [NSString stringWithFormat:@"%d.JPG",i];        UIImage *image = [UIImage imageNamed:imgName];        [mutArrary addObject:image];    }        rootCtrl.images = mutArrary;        self.window.rootViewController = navCtrl;        return YES;}@end
Rootviewcontroller. h

@ Interface rootviewcontroller: uiviewcontroller <uiscrollviewdelegate> {nsinteger _ index;} @ property (nonatomic, retain) nsarray * images; // store the displayed Image

Rootviewcontroller. m

# Import "rootviewcontroller. H "# import" photoscrollview. H "@ interface rootviewcontroller () @ end @ implementation rootviewcontroller-(ID) initwithnibname :( nsstring *) bundle :( nsbundle *) handle {self = [Super initwithnibname: nibnameornil Bundle: nibbundleornil]; If (Self) {// custom initialization} return self;}-(void) viewdidload {[Super viewdidload]; // create a rolling view uiscrollview * scrollvi Ew = [[uiscrollview alloc] initwithframe: cgrectmake (0, 0,340,480)]; // hide the horizontal scroll bar scrollview. showshorizontalscrollindicator = no; scrollview. backgroundcolor = [uicolor blackcolor]; scrollview. delegate = self; // sets the page effect scrollview. pagingenabled = yes; // sets the content size to scrollview. contentsize = cgsizemake (340 * _ images. count, 480); [self. view addsubview: scrollview]; for (INT I = 0; I <self. images. count; I ++) {photo Scrollview * photoview = [[photoscrollview alloc] initwithframe: cgrectmake (340 * I, 0,320,480)]; photoview. tag = I + 100; // pass the value to photoview. image = _ images [I]; [scrollview addsubview: photoview] ;}# Pragma mark-uiscrollview delegate-(void) scrollviewdidenddecelerating :( uiscrollview *) scrollview {// 1. get the current page number int currentpage = scrollview. contentoffset. x/340; // 2. restore previous scaling attempts if (currentpage! = _ Index) {// obtain the view int tag = _ index + 100; photoscrollview * view = (photoscrollview *) [scrollview viewwithtag: Tag]; // restore [view setzoomscale: 1];} // 3. record the current page number _ Index = currentpage;}/* # pragma mark-Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation-(void) prepareforsegue :( uistoryboardsegue *) segue sender :( ID) sender {// get the New View Controller Using [segue destinationviewcontroller]. // pass the selected object to the New View Controller .} * // @ end

Photoscrollview. h

@interface PhotoScrollView : UIScrollView<UIScrollViewDelegate>{    UIImageView *_imageView;}@property(nonatomic, retain)UIImage *image;

Photoscrollview. m

# Import "photoscrollview. H "@ implementation photoscrollview-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; If (Self) {_ imageview = [[uiimageview alloc] initwithframe: self. bounds]; // data cannot be set here // _ imageview. image = _ image; [self addsubview: _ imageview]; // you can specify the maximum magnification. maximumzoomscale = 2.0; // you can specify the minimum zoomscale. minimumzoomscale =. 5; // hide the scroll bar self. showshorizontalscrollindicator = no; self. showsverticalscrollindicator = no; // sets the proxy self. delegate = self; // Add gesture uitapgesturerecognizer * tap = [[uitapgesturerecognizer alloc] initwithtarget: Self action: @ selector (tapactoin :)]; tap. numberoftapsrequired = 2; [self addgesturerecognizer: tap];} return self;} // gesture RESPONSE event-(void) tapactoin :( uitapgesturerecognizer *) tap {If (self. zoomscale> 1) {// zoom out [self setzoomscale: 1 animated: Yes];} else {// zoom in [self setzoomscale: 2 animated: Yes];}-(void) setimage :( uiimage *) image {_ image = image; _ imageview. image = _ image ;}# Pragma mark-uiscrollview delegate-(uiview *) viewforzoominginscrollview :( uiscrollview *) scrollview {return _ imageview;} @ end



Simple image browser [zoomed images, sliding back to normal]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.