UIScrollView可幫助我們在手機顯示的小畫面中瀏覽大圖,
將大圖複製進xcode後,在套入以下程式碼即可執行。
viewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIScrollViewDelegate>
{
UIImageView *imageView;
UILabel *scaleRatioLabel;
}
@end
viewController.m
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImage *myImage = [UIImage imageNamed:@"2013-06-13 20.02.00.jpg"];
imageView = [[UIImageView alloc]initWithImage:myImage];
[imageView setFrame:CGRectMake(0, 0, 2268, 568)];
[_myScrollView addSubview:imageView];
_myScrollView.contentSize = myImage.size;
_myScrollView.minimumZoomScale = 1.0f;
_myScrollView.maximumZoomScale = 5.0f;
_myScrollView.zoomScale = 1.0f;
scaleRatioLabel = [[UILabel alloc]initWithFrame:CGRectMake(320 / 2 - 50 / 2, 568 / 2 - 25 / 2, 50, 25)];
[scaleRatioLabel setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:scaleRatioLabel];
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
[self.view bringSubviewToFront:scaleRatioLabel];
[scaleRatioLabel setAlpha:0.6f];
[scaleRatioLabel setBackgroundColor:[UIColor lightGrayColor]];
scaleRatioLabel.text = [NSString stringWithFormat:@"x%.1f", scale];
[UIView transitionWithView:scaleRatioLabel duration:2.0f options:UIViewAnimationOptionCurveEaseInOut animations:^(void){scaleRatioLabel.alpha = 0.0f;} completion:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
@end
文章標籤
全站熱搜