加载Xib文件
-
方法1
NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"Test" owner:nil options:nil];
[self.view addSubview:objs[1]];
objc
XMGShopView *shopView = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
```
-
方法2
// 一个UINib对象就代表一个xib文件
//UINib *nib = [UINib nibWithNibName:@"Test" bundle:[NSBundle mainBundle]];
//一般情况下,bundle参数传nil,默认就是mainBundle
UINib *nib = [UINib nibWithNibName:@"Test" bundle:nil];
NSArray *objs = [nib instantiateWithOwner:nil options:nil];
[self.view addSubview:[objs lastObject]];
```
UIScrollView
1.属性
(1) contentSize
- 这个属性用来表示UIScrollView内容的尺寸,滚动范围(能滚多远)
- 如果想禁止某个方向的滚动,那么久可以直接设置width或者height=0
- 如果UIScrollView无法滚动,可能是以下原因:
- 没有设置contentSize
- scrollEnabled = NO
- 没有接收到触摸事件:userInteractionEnabled = NO
self.scrollView.contentSize = CGSizeMake(400, 250);
(2) contentOffset
- 这个属性用来表示UIScrollView滚动的位置(其实就是内容左上角与scrollView左上角的 X\Y差值)
-
self.scrollView.contentOffset.y = offsetY;(OC语法细节:不允许直接修改OC对象的结构体属性的)
- (IBAction)leftClick:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
//// self.scrollView.contentOffset : 偏移量
self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentOffset.y);
}];
}
- (IBAction)topClick:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, 0);
}];
}
- (IBAction)buttonClick:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentOffset.x, self.scrollView.contentSize.height - self.scrollView.frame.size.height);
}];
}
- (IBAction)rightClick:(id)sender {
[UIView animateWithDuration:0.5 animations:^{
self.scrollView.contentOffset = CGPointMake(self.scrollView.contentSize.width - self.scrollView.frame.size.width, self.scrollView.contentOffset.y);
}];
}
(3) contentInset
- 这个属性能够在UIScrollView的4周增加额外的滚动区域,一般用来避免scrollView的内容被其他控件挡住
// 设置contentInset
self.scrollView.contentOffset = CGPointMake(0, -64);
self.scrollView.contentInset = UIEdgeInsetsMake(64, 0, 0, 0);
(4) UIScrollView 的其他属性
-
@property(nonatomic) BOOL bounces;
设置UIScrollView是否需要弹簧效果
-
@property(nonatomic,getter=isScrollEnabled) BOOL scrollEnabled;
设置UIScrollView是否能滚动
-
@property(nonatomic) BOOL showsHorizontalScrollIndicator;
是否显示水平滚动条
-
@property(nonatomic) BOOL showsVerticalScrollIndicator;
是否显示垂直滚动条
self.scrollView.showsHorizontalScrollIndicator = NO;
self.scrollView.showsVerticalScrollIndicator = NO;
2.代理UIScrollViewDelegate
- 用户开始拖拽时调用scrollViewWillBeginDragging
- 滚动到某个位置时调用scrollViewDidScroll
- 用户结束拖拽时调用scrollViewDidEndDragging
- (减速完毕)由于惯性停止滚动的时候调用scrollViewDidEndDecelerating
- 这个方法的返回值决定了要缩放的内容(返回值只能是UIScrollView的子控件) viewForZoomingInScrollView
设置缩放比例
self.scrollview.maximumZoomScale = 2.0;
self.scrollview.minimumZoomScale = 0.2;
- 缩放开始的时候调用scrollViewWillBeginZooming
- 正在缩放的时候调用scrollViewDidZoom
分页
#pragma mark - pageScrollView
- (void)setupPageScrollView
{
self.pageScrollView.delegate = self;
NSInteger count = 5;
CGFloat imageW = self.pageScrollView.frame.size.width;
CGFloat imageH = self.pageScrollView.frame.size.height;
for (int i = 0; i < count; i++) {
NSString *imageNamed = [NSString stringWithFormat:@"img_%02d",i];
UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:imageNamed]];
// 计算图片位置
CGFloat imageX = i * imageW;
image.frame = CGRectMake(imageX, 0, imageW, imageH);
[self.pageScrollView addSubview:image];
}
// 设置contentSize
self.pageScrollView.contentSize = CGSizeMake(imageW * count, 0);
// 开启分页功能
self.pageScrollView.pagingEnabled = YES;
// 总页数
self.pageControl.numberOfPages = count;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSLog(@"滚动中");
// 分页scrollView
if(scrollView == self.pageScrollView){
// 0.3 > (int)(0.3 + 0.5) > 0
// 0.6 > (int)(0.6 + 0.5) > 1
// 小数四舍五入为整数 : (int)(小数 + 0.5)
int page = (int)(self.pageScrollView.contentOffset.x / self.pageScrollView.frame.size.width + 0.5);
self.pageControl.currentPage = page;
}
}