DelegateScrollView ScrollView;
-(void)viewDidLoad
{
[super viewDidLoad];
ScrollView = [[DelegateScrollView alloc] initWithFrame:CGRectMake(0,- self.view.frame.size.height, self.view.frame.size.width, self.view.frame.size.height)];
ScrollView.delegate = ScrollView;
ScrollView.scrollEnabled = YES;
ScrollView.backgroundColor = [UIColor blackColor ];
ScrollView.maximumZoomScale = 5.0f;
ScrollView.minimumZoomScale = 1.0f;
[self.view addSubview:ScrollView];
[ScrollView setZoomScale:2 animated:YES];
}
DelegateScrollView.h:
@interface ChildDelegateScrollView : UIScrollView <UIScrollViewDelegate>
@end
并且zoom永远不会发生我也在ViewController.h中试过这个:@interface ViewController : UIViewController <UIScrollViewDelegate>
然后设置委托就像这个ScrollView.delegate = self;
一样无法正常工作,如何正确设置ScrollView的委托?
答案 0 :(得分:10)
UIScrollView class reference中的这一行似乎是相关的:
要进行缩放和平移,代理必须实现viewForZoomingInScrollView:和scrollViewDidEndZooming:withView:atScale:
我能够在UIScrollView上获取UILabel,以使用此UIViewController代码进行缩放:
#import "ViewController.h"
@interface ViewController () <UIScrollViewDelegate> {
UIScrollView *_scrollView;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.frame.size.width, self.view.frame.size.height)];
scrollView.delegate = self;
scrollView.scrollEnabled = YES;
scrollView.backgroundColor = [UIColor blackColor];
scrollView.maximumZoomScale = 5.0f;
scrollView.minimumZoomScale = 1.0f;
scrollView.contentSize = CGSizeMake(scrollView.frame.size.width, scrollView.frame.size.height);
[self.view addSubview:scrollView];
_scrollView = scrollView;
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50.0, 50.0, 100.0, 25.0)];
label.backgroundColor = [UIColor grayColor];
label.textAlignment = NSTextAlignmentCenter;
label.text = @"Test";
[_scrollView addSubview:label];
}
- (void)viewDidAppear:(BOOL)animated
{
[_scrollView setZoomScale:4.0 animated:YES];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return _scrollView.subviews.firstObject;
}
- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale
{
}
@end