二、iOS常用控件

UICollectionView

1、声明属性
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *dataSource;
2、遵守协议
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
	return self.dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
	LCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LCCell" forIndexPath:indexPath];
    
	return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    
	if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
		LCHHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"LCHHeader" forIndexPath:indexPath];
		return header;
	} else {
		LCFooter *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"LCFooter" forIndexPath:indexPath];
		return footer;
	}
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
	LCCell *cell = (LCCell *)[collectionView cellForItemAtIndexPath:indexPath];
	// TUDO:
}
3、getter方法
- (UICollectionView *)collectionView {
    
	if (_collectionView == nil) {
		CGFloat space = 1;
		UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
		layout.scrollDirection = UICollectionViewScrollDirectionVertical;
		layout.minimumLineSpacing = space;
		layout.minimumInteritemSpacing = space;
		layout.sectionInset = UIEdgeInsetsMake(space, space, space, space);
		CGFloat width = self.view.bounds.size.width;
		layout.headerReferenceSize = CGSizeMake(width, width * 200 / 320);
		layout.footerReferenceSize = CGSizeMake(width, 40);
		CGFloat itemWidth = (width - layout.sectionInset.left - layout.sectionInset.right - space * (4 - 1)) / 4;
		layout.itemSize = CGSizeMake(itemWidth, itemWidth);
		_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
		_collectionView.backgroundColor = [UIColor whiteColor];
		_collectionView.delegate = self;
		_collectionView.dataSource = self;
		_collectionView.showsVerticalScrollIndicator = NO;
		_collectionView.showsHorizontalScrollIndicator = NO;
		[self.view addSubview:_collectionView];
		[_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
		    make.left.right.top.bottom.equalTo(self.view);
		}];
		[_collectionView registerClass:[LCCell class]
		    forCellWithReuseIdentifier:@"LCCell"];
		[_collectionView registerClass:[LCHeader class]
		    forSupplementaryViewOfKind:UICollectionElementKindSectionHeader
		           withReuseIdentifier:@"LCHeader"];
		[_collectionView registerClass:[LCFooter class]
		    forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
		           withReuseIdentifier:@"LCFooter"];
	}
	
	return _collectionView;
}
- (NSArray *)dataSource {
    
	if (_dataSource == nil) {
		_dataSource = @[];
	}
    
	return _dataSource;
}

UITableView

1、声明属性
@property (nonatomic, strong) UITableView *tableView;
2、遵守协议
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
	return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XXXCell"];
	if (!cell) {
		cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"XXXCell"];
	}

	return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
	// TUDO:
}
3、getter方法
- (UITableView *)tableView {
    
	if (_tableView == nil) {
		_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
		_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
		_tableView.dataSource = self;
		_tableView.delegate = self;
		_tableView.rowHeight = 60;
		[self.view addSubview:_tableView];
		[_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
			make.left.right.top.bottom.equalTo(self.view);
		}];
	}
	
	return _tableView;
}

手势
添加手势

// 左滑
UISwipeGestureRecognizer *leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)];
leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftSwipe];
// 右滑
UISwipeGestureRecognizer *rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)];
rightSwipe.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:rightSwipe];
// 单击
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)];
singleTap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:singleTap];
// 双击
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)];
doubleTap.numberOfTapsRequired = 2;
[self.view addGestureRecognizer:doubleTap];
// 拖拽
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)];
[self.view addGestureRecognizer:pan];
// 缩放
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)];
[self.view addGestureRecognizer:pinch];
// 旋转
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)];
[self.view addGestureRecognizer:rotation];
// 长按
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(processgestureReconizer:)];
longPress.minimumPressDuration = 2.0;
[self.view addGestureRecognizer:longPress];

部分手势处理

- (void)processgestureReconizer:(UIGestureRecognizer *)gesture {
    
    // 拖拽
    if ([gesture isKindOfClass:[UIPanGestureRecognizer class]]) {
	    // UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gesture;
	    // CGPoint point = [pan translationInView:pan.view];
	    // pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
	    // [pan setTranslation:CGPointZero inView:pan.view];
        UIPanGestureRecognizer *pan = (UIPanGestureRecognizer *)gesture;
        CGPoint startCenter = CGPointZero;
        if (pan.state == UIGestureRecognizerStateBegan) {
            startCenter = gesture.view.center;
        } else if (pan.state == UIGestureRecognizerStateChanged) {
            // 此处必须从self.view中获取translation,因为translation和view的transform属性挂钩,若transform改变了则translation也会变
            CGPoint translation = [pan translationInView:self.view];
            gesture.view.center = CGPointMake(startCenter.x + translation.x, startCenter.y + translation.y);
        } else if (pan.state == UIGestureRecognizerStateEnded) {
            startCenter = CGPointZero;
        }
    }
    // 缩放
    if ([gesture isKindOfClass:[UIPinchGestureRecognizer class]]) {
        UIPinchGestureRecognizer *pinch = (UIPinchGestureRecognizer *)gesture;
        CGFloat startScale = 1;
        if (pinch.state == UIGestureRecognizerStateBegan) {
            startScale = pinch.scale;
        } else if (pinch.state == UIGestureRecognizerStateChanged) {
            CGFloat scale = (pinch.scale - startScale) / 2 + 1;
            gesture.view.transform = CGAffineTransformScale(gesture.view.transform, scale, scale);
            startScale = pinch.scale;
        } else if (pinch.state == UIGestureRecognizerStateEnded) {
            startScale = 1;
        }
    }
    // 旋转
    if ([gesture isKindOfClass:[UIRotationGestureRecognizer class]]) {
        UIRotationGestureRecognizer *rotate = (UIRotationGestureRecognizer *)gesture;
        CGFloat startRotation = 0;
        if (rotate.state == UIGestureRecognizerStateBegan) {
            startRotation = rotate.rotation;
        } else if (rotate.state == UIGestureRecognizerStateChanged) {
            CGFloat rotation = (rotate.rotation - startRotation) / 2;
            gesture.view.transform = CGAffineTransformRotate(gesture.view.transform, rotation);
            startRotation = rotate.rotation;
        } else if (rotate.state == UIGestureRecognizerStateEnded) {
            startRotation = 0;
        }
    }
}

UITabBarController

// 通过此方法能够让子控制器的 NavigationItem 替换 TabBarController 的 NavigationItem
- (void)setSelectedViewController:(UIViewController *)selectedViewController {
    [super setSelectedViewController:selectedViewController];
    
    if (nil != selectedViewController.navigationItem.titleView) {
        self.navigationItem.titleView = selectedViewController.navigationItem.titleView;
    } else {
        self.navigationItem.titleView = nil;
        self.navigationItem.title = selectedViewController.navigationItem.title;
    }
    self.navigationItem.leftBarButtonItem = selectedViewController.navigationItem.leftBarButtonItem;
    self.navigationItem.leftBarButtonItems = selectedViewController.navigationItem.leftBarButtonItems;
    self.navigationItem.rightBarButtonItem = selectedViewController.navigationItem.rightBarButtonItem;
    self.navigationItem.rightBarButtonItems = selectedViewController.navigationItem.rightBarButtonItems;
}

基于GCD的倒计时按钮

// 基于GCD的倒计时按钮
- (void)countdown:(int)maxTime button:(UIButton *)button {
    
    // 关闭按钮的交互
    button.userInteractionEnabled = NO;
    __block int timeout = (int)maxTime;
    __weak typeof(button) weakButton = button;
    // 获取全局子线程队列
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    // 创建timer添加到队列中
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    // 设置时间间隔
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0);
    // 处理事件block
    dispatch_source_set_event_handler(timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            timeout--;
            [weakButton setTitle:timeout > 0 ? [NSString stringWithFormat:@"%d s", timeout] : @"Resend" forState:UIControlStateNormal];
            weakButton.userInteractionEnabled = timeout > 0 ? NO : YES;
            // 倒计时结束,关闭定时器
            if (timeout <= 0) {
                dispatch_source_cancel(timer);
            }
        });
    });
    // 定时器
    dispatch_resume(timer);
}

你可能感兴趣的:(iOS)