上篇博客已經介紹了如何使用UITableView橫屏滑動,本篇博客加上了點擊放大的操作,是放在UITableViewCell的cell上的,因為可視化控件太多了,故此隱藏掉,使用方法也很簡單.userInteractionEnabled = false就可以了。
1.點擊UIButton中的圖片放大,首先要寫UIButton的方法
2.改變控件大小的方法有很多,一個改變之后的大小,一個之前的大小
3.默認是第一個選中,跟單選很像,那就定義一個標記,然后每次點擊替換標記,然后做對比是不是同一個標記
4.然后就是刷新UITableView
#import "ViewController.h"
#import "FFTableViewCell.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UITableView *myTableView;
@property(strong,nonatomic)NSIndexPath *selectedIndexPath;
@end
@implementation ViewController
- (UITableView *)myTableView{
if(!_myTableView){
CGRect tableViewRect = CGRectMake(0, 0,100, CGRectGetWidth(self.view.frame));
_myTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
_myTableView.dataSource = self;
_myTableView.delegate = self;
_myTableView.frame = tableViewRect;
_myTableView.separatorStyle = NO;
_myTableView.backgroundColor = [UIColor grayColor];
_myTableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
_myTableView.showsVerticalScrollIndicator = NO;
_myTableView.center = CGPointMake(self.view.frame.size.width / 2, 50);
}
return _myTableView;
}
- (void)viewDidLoad {
[super viewDidLoad];
//AppDelegate 進行全局設置
if (@available(iOS 11.0, *)){
[[UIScrollView appearance] setContentInsetAdjustmentBehavior:UIScrollViewContentInsetAdjustmentNever];
}
self.view.backgroundColor = [UIColor purpleColor];
[self.view addSubview:self.myTableView];
self.selectedIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
// Do any additional setup after loading the view, typically from a nib.
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 100;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
FFTableViewCell *cell = [FFTableViewCell cellWithTableView:tableView];
if([self.selectedIndexPath isEqual:indexPath]){
[cell zoomPtoto:YES];
}else{
[cell zoomPtoto:NO];
}
return cell;
}
#pragma mark 選中的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
self.selectedIndexPath = indexPath;
[self.myTableView reloadData];
}
- (void)dealloc{
_myTableView = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import "FFTableViewCell.h"
@interface FFTableViewCell()
@property(strong,nonatomic)UIButton *monthBtn;
@end
@implementation FFTableViewCell
static NSString *cellID = @"FFTableViewCell";
- (UIButton *)monthBtn{
if(!_monthBtn){
_monthBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_monthBtn.backgroundColor = [UIColor redColor];
_monthBtn.layer.cornerRadius = 20.0f;
_monthBtn.clipsToBounds = YES;
[_monthBtn setTitle:@"在干嘛" forState:UIControlStateNormal];
_monthBtn.titleLabel.font = [UIFont systemFontOfSize:11.0f];
_monthBtn.userInteractionEnabled = false;
}
return _monthBtn;
}
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
FFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[FFTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if(self){
self.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2);
[self.contentView addSubview:self.monthBtn];
}
return self;
}
- (void)zoomPtoto:(BOOL)isBool{
_isBool = isBool;
if(isBool){
_monthBtn.layer.cornerRadius = 30.0f;
_monthBtn.frame = CGRectMake((CGRectGetWidth(self.contentView.frame)-60)/2, (CGRectGetHeight(self.contentView.frame)-60)/2, 60,60);
}else{
_monthBtn.layer.cornerRadius = 20.0f;
_monthBtn.frame = CGRectMake((CGRectGetWidth(self.contentView.frame)-40)/2, (CGRectGetHeight(self.contentView.frame)-40)/2, 40,40);
}
}
- (void)layoutSubviews{
[super layoutSubviews];
if(_isBool){
_monthBtn.layer.cornerRadius = 30.0f;
_monthBtn.frame = CGRectMake((CGRectGetWidth(self.contentView.frame)-60)/2, (CGRectGetHeight(self.contentView.frame)-60)/2, 60,60);
}else{
_monthBtn.layer.cornerRadius = 20.0f;
_monthBtn.frame = CGRectMake((CGRectGetWidth(self.contentView.frame)-40)/2, (CGRectGetHeight(self.contentView.frame)-40)/2, 40,40);
}
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
- (void)dealloc{
_monthBtn = nil;
}
@end免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。