Objective C examples sought
I'm trying to add scrollbars to an IKImageView. Basically at the moment, I need some examples of a program that loads an image into a view, and if the window is too small, sets up scrollbar开发者_运维技巧s that do the right things...
Why can I not find these examples on the apple dev site?
Added info:
After looking at ImagekitDemo I see that evidently I DO need to embed the IKIMageView in a ScrollView. (and somehow that makes the has___Scroller properties of the IKImageView YES...)
However, now (And this is true in ImageKitDemo as well) The scroll bars are fine as long as only one (or neither) is needed. However, as soon as both are needed, and either dimension of the window is smaller than the image, BOTH scrollbars disappear.
Mouse scrolling still works.
ZoomViewController.h
@interface ZoomViewController : UIViewController <UIScrollViewDelegate>
{
ForecastImage* detailImage; // wrapper class around UIImage (optional - could just be UIImage)
IBOutlet UIImageView* imageView;
IBOutlet DoubleTapScrollView* zoomableScrollView;
}
@property (readwrite, nonatomic, retain) ForecastImage* detailImage;
- (IBAction) dismissZoomViewController;
- (UIView *) viewForZoomingInScrollView:(UIScrollView *)scrollView;
- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale;
@end
ZoomViewController.m
@implementation ZoomViewController
@synthesize detailImage;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[imageView setImage:[self.detailImage renderedImage]];
[zoomableScrollView setContentSize:[[imageView image] size]];
[zoomableScrollView setMaximumZoomScale:5.0];
[zoomableScrollView setMinimumZoomScale:0.25];
}
- (void) viewDidAppear:(BOOL)animated
{
self.navigationItem.title = [SearchService getDisplayName:[self.detailImage forecastArea]];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc
{
imageView.image = nil;
self.detailImage = nil;
[super dealloc];
}
- (IBAction) dismissZoomViewController
{
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark Pinch-n-Zoom
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return imageView;
}
- (void) scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale
{
CGSize newSize = CGSizeMake(imageView.image.size.width * scale, imageView.image.size.height * scale);
[scrollView setContentSize:newSize];
}
@end
The best place to start is the Scroll View Programming Guide. Basically, you need to put the IKImageView inside a NSScrollView. If the IKImageView size exceeds the visible rectangle of the NSScrollView, then scrollbars will appears.
The following sample uses IKImageView to perform various zoom and resizing operations.
精彩评论