Hi,
I've implemented custom UIScrollView with images. But on dragging it crashes.
Here is a code:
Where is a mistake?
Thank you.
I've implemented custom UIScrollView with images. But on dragging it crashes.
Here is a code:
PHP:
const CGFloat kScrollObjHeight = 100.0;
const CGFloat kScrollObjWidth = 100.0;
const NSUInteger kNumImages = 7;
- (void)viewDidLoad {
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
[_scrollView setBackgroundColor:[UIColor whiteColor]];
[_scrollView setCanCancelContentTouches:NO];
_scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
_scrollView.clipsToBounds = YES; // default is NO, we want to restrict drawing within our scrollview
_scrollView.scrollEnabled = YES;
_scrollView.minimumZoomScale = 1;
_scrollView.maximumZoomScale = 3;
_scrollView.delegate = self;
[_scrollView setScrollEnabled:YES];
[_scrollView setContentSize:[self loadImagesFromBundle]];
_scrollView.pagingEnabled = YES;
}
// load all the images from bundle
// and add them to the scroll view
- (CGSize)loadImagesFromBundle {
CGSize contentSize;
contentSize.height = kScrollObjHeight;
contentSize.width = 0;
const int kGap = 20;
for (int i = 1; i <= kNumImages; i++)
{
NSString *imageName = [NSString stringWithFormat:@"photo%d.png", i];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
// setup each frame to a default height and width, it will be properly placed when we call "updateScrollList"
CGRect rect = imageView.frame;
rect.size.height = kScrollObjHeight;
rect.size.width = kScrollObjWidth;
rect.origin.x = contentSize.width;
rect.origin.y = kGap;
contentSize.width = contentSize.width + kGap + kScrollObjWidth;
imageView.frame = rect;
imageView.tag = i; // tag our images for later use when we place them in serial fashion
[_scrollView addSubview:imageView];
[imageView release];
}
return contentSize;
}
PHP:
@interface ScrollViewController : UIViewController <UIScrollViewDelegate> {
IBOutlet UIScrollView *_scrollView;
}
- (CGSize)loadImagesFromBundle;
@property (retain) UIScrollView* _scrollView;
@end
Thank you.