UISrollView
UIScrollView的基本使用
UIScrollView的用法很简单
将需要展示的内容添加到UIScrollView中
设置UIScrollView的contentSize属性,告诉UIScrollView所有内容的尺寸,也就是告诉它滚动的范围(能滚多远,滚到哪里是尽头)
UIScrollView显示内容的小细节
超出UIScrollView边框的内容会被自动隐藏
用户可以用过手势拖动来查看超出边框并被隐藏的内容
self.scrollView.contentSize=CGSizeMake(0, 900);
self.scrollView.scrollEnabled=YES;
UIScrollView无法滚动的解决办法
如果UIScrollView无法滚动,可能是以下原因:
没有设置contentSize
scrollEnabled = NO
没有接收到触摸事件:userInteractionEnabled = NO
… …
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"minion"]];
[self.scrollView addSubview:imageView];
self.scrollView.contentSize=imageView.image.size;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)left:(id)sender {
self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y);
}
- (IBAction)top:(id)sender {
self.scrollView.contentOffset=CGPointMake(self.scrollView.contentOffset.y, 0);
}
- (IBAction)bottom:(id)sender {
[UIView beginAnimations:nil context:nil];
CGFloat offSetY=self.scrollView.contentSize.height-self.scrollView.frame.size.height;
self.scrollView.contentOffset=CGPointMake(self.scrollView.contentOffset.y, offSetY);
[UIView commitAnimations];
}
- (IBAction)right:(id)sender {
[UIView beginAnimations:nil context:nil];
CGFloat offsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;
self.scrollView.contentOffset = CGPointMake(offsetX, self.scrollView.contentOffset.y);
[UIView commitAnimations];
}
@end