adi*_*dit 27 iphone objective-c uiscrollview ipad ios
我是否真的需要在UIScrollView中使用UIPinchGestureRecognizer才能使捏合工作?如果是,我该怎么办?我正在尝试实现flipboard所具有的功能,它基本上放大图像并在放大后具有滚动功能.我该怎么做?
更新:
这里有一些我没有调用滚动视图委托的代码
CGRect imgFrame;
imgFrame.size.width = originalImageSize.width;
imgFrame.size.height = originalImageSize.height;
imgFrame.origin.x = imageOriginPoint.x;
imgFrame.origin.y = imageOriginPoint.y;
NSData *data = [request responseData];
UIImage * image = [UIImage imageWithData:data];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setUserInteractionEnabled:YES];
[imageView setBackgroundColor:[UIColor clearColor]];
[imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[imageView setFrame:CGRectMake(0, 0, imgFrame.size.width, imgFrame.size.height)];
UIScrollView * imgScrollView = [[UIScrollView alloc] initWithFrame:imageView.frame];
imgScrollView.delegate = self;
imgScrollView.showsVerticalScrollIndicator = NO;
imgScrollView.showsHorizontalScrollIndicator = NO;
[imgScrollView setScrollEnabled:YES];
[imgScrollView setClipsToBounds:YES];
[imgScrollView addSubview:imageView];
[imgScrollView setBackgroundColor:[UIColor blueColor]];
[imgScrollView setMaximumZoomScale:1.0];
Run Code Online (Sandbox Code Playgroud)
Ign*_*ese 91
您需要做的就是在您的内部添加您的UIImageView(或任何想要缩放的视图)UIScrollView.
设置你maximumZoomScale对你UIScrollView比1.0F更高的任意值.
将自己设置为您的委托UIScrollView并返回UIImageViewviewForZooming委托方法.
而已.不需要捏手势,没有任何东西.UIScrollView处理捏变焦为你.
Shr*_*ade 17
Scrollview中的Swift 2.0 Pinch-zoom图像(Swift 2.0)
1.滚动视图约束
2.在ScrollView中添加ImageView并设置约束
3.拿IBOutlets
IBOutlet UIScrollView * bgScrollView;
IBOutlet UIImageView * imageViewOutlet;
Run Code Online (Sandbox Code Playgroud)
4. viewDidLoad方法
- (void)viewDidLoad
{
[super viewDidLoad];
float minScale=bgScrollView.frame.size.width / imageViewOutlet.frame.size.width;
bgScrollView.minimumZoomScale = minScale;
bgScrollView.maximumZoomScale = 3.0;
bgScrollView.contentSize = imageViewOutlet.frame.size;
bgScrollView.delegate = self;
}
Run Code Online (Sandbox Code Playgroud)
5.滚动查看委派方法
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageViewOutlet;
}
Run Code Online (Sandbox Code Playgroud)
Nov*_*arg 15
不久之前我没有使用捏合识别器做了自定义图像查看器.在UIScrollView上只有UIImageView.你传递一个带有图像链接的字符串,它还有一个进度条.一旦图像完成加载,就会显示图像.这是代码:
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.theImageView;
}
- (CGRect)centeredFrameForScrollView:(UIScrollView *)scroll andUIView:(UIView *)rView {
CGSize boundsSize = scroll.bounds.size;
CGRect frameToCenter = rView.frame;
// center horizontally
if (frameToCenter.size.width < boundsSize.width) {
frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width) / 2;
}
else {
frameToCenter.origin.x = 0;
}
// center vertically
if (frameToCenter.size.height < boundsSize.height) {
frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height) / 2;
}
else {
frameToCenter.origin.y = 0;
}
return frameToCenter;
}
-(void)scrollViewDidZoom:(UIScrollView *)scrollView
{
self.theImageView.frame = [self centeredFrameForScrollView:self.theScrollView andUIView:self.theImageView];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.resourceData setLength:0];
self.filesize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.resourceData appendData:data];
NSNumber *resourceLength = [NSNumber numberWithUnsignedInteger:[self.resourceData length]];
self.progressBar.progress = [resourceLength floatValue] / [self.filesize floatValue];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
self.theImage = [[UIImage alloc]initWithData:resourceData];
self.theImageView.frame = CGRectMake(0, 0, self.theImage.size.width, self.theImage.size.height);
self.theImageView.image = self.theImage;
self.theScrollView.minimumZoomScale = self.theScrollView.frame.size.width / self.theImageView.frame.size.width;
self.theScrollView.maximumZoomScale = 2.0;
[self.theScrollView setZoomScale:self.theScrollView.minimumZoomScale];
self.theScrollView.contentSize = self.theImageView.frame.size;
self.theLabel.hidden = YES;
self.progressBar.hidden = YES;
}
-(void)setImageInImageView
{
NSURLRequest *req = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:self.imageLink]];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:req delegate:self];
if (conn)
{
self.resourceData = [NSMutableData data];
}
else
{
NSLog(@"Connection failed: IMageViewerViewController");
}
}
-(void)loadView
{
self.filesize = [[NSNumber alloc]init];
self.progressBar = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleBar];
self.progressBar.frame = CGRectMake(20, 240, 280, 40);
[self.progressBar setProgress:0.0];
self.theImageView = [[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]autorelease];
self.theScrollView = [[[UIScrollView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]]autorelease];
self.theScrollView.delegate = self;
[self.theScrollView addSubview:self.theImageView];
self.view = self.theScrollView;
self.theLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, 320, 40)];
self.theLabel.font = [UIFont boldSystemFontOfSize:15.0f];
self.theLabel.text = @"Please wait, file is being downloaded";
self.theLabel.textAlignment = UITextAlignmentCenter;
self.theLabel.hidden = NO;
[self.view addSubview:self.progressBar];
[self.view bringSubviewToFront:self.progressBar];
[self.view addSubview:self.theLabel];
[self.view bringSubviewToFront:self. theLabel];
[self performSelectorOnMainThread:@selector(setImageInImageView) withObject:nil waitUntilDone:NO];
}
Run Code Online (Sandbox Code Playgroud)
和头文件:
@interface ImageViewerViewController : UIViewController<UIScrollViewDelegate, NSURLConnectionDelegate, NSURLConnectionDataDelegate>
@property (nonatomic, retain) IBOutlet UIImageView *theImageView;
@property (nonatomic, retain) IBOutlet UIScrollView *theScrollView;
@property (nonatomic, retain) NSString *imageLink;
@property (nonatomic, retain) UIImage *theImage;
@property (nonatomic, retain) UILabel *theLabel;
@property (nonatomic, retain) UIProgressView *progressBar;
@property (nonatomic, retain) NSMutableData *resourceData;
@property (nonatomic, retain) NSNumber *filesize;
@end
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你
我想这个答案可能是不必要的,但我遇到了与@adit类似的问题,并以一种非常简单的方式解决了它(我认为),也许有类似挑战的人可以使用它:
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor yellowColor];
UIImage *imageToLoad = [UIImage imageNamed:@"background_green"];
self.myImageView = [[UIImageView alloc]initWithImage:imageToLoad];
self.myScrollView = [[UIScrollView alloc]initWithFrame:self.view.bounds];
[self.myScrollView addSubview:self.myImageView];
self.myScrollView.contentSize = self.myImageView.bounds.size;
self.myScrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
self.myScrollView.minimumZoomScale = 0.3f;
self.myScrollView.maximumZoomScale = 3.0f;
self.myScrollView.delegate = self;
[self.view addSubview:self.myScrollView];
}
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView {
NSLog(@"viewForZoomingInScrollView");
return self.myImageView;
}
Run Code Online (Sandbox Code Playgroud)
我的问题很简单,我忘记添加self.myScrollView.delegate = self;,这就是我遇到问题的原因.我花了很长时间才想出这个简单的问题,我想我没有看到所有树木的福雷斯特:-)