前陣子工作不忙,正好自己又想要做圖片和文字的混編,雖然知道類似的軟件有很多扶认,但是因為平時幾乎只用原生的相機唇跨,所以對五花八門的相機也不了解狭吼,所以就想著自己來實現(xiàn)這個功能层坠。
這個功能的設計思路版本是:
1.最開始只打算加一張圖片和一段文字,所以就很死板刁笙,都是固定好的破花,沒什么新意;
2.后來想著一張圖片一段文字的固定格式疲吸,還是覺得不人性化座每,就想著任由使用者隨意添加圖片和文字,并且如果第一個添加的控件是textview磅氨,就默認文本居中尺栖,類似標題的存在;
3.第2個功能實現(xiàn)后烦租,有一天晚上編輯圖片給朋友的時候延赌,發(fā)現(xiàn)一旦添加好不想要了就得重新開頭做,然后就想著再添加個可以刪除imageview和textview的功能叉橱,并且imageview可以修改圖片挫以;
4.做完第三個功能后,覺得大致實現(xiàn)了窃祝,然后突然想到我可以再添加一個拖動textview改變頁面布局的功能掐松;
5.增加了添加輸入框前先選擇文本對齊方式的功能;
6.完成粪小。
最終效果預覽
textview相關
//文本按鈕
-(UIButton *)textButton{
if (_textButton==nil) {
_textButton=[UIButton buttonWithType:UIButtonTypeCustom];
[_textButton setImage:[UIImage imageNamed:@"text"] forState:UIControlStateNormal];
_textButton.layer.cornerRadius=25;
_textButton.layer.masksToBounds=YES;
_textButton.backgroundColor=[UIColor whiteColor];
[_textButton addTarget:self action:@selector(textButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _textButton;
}
//文本按鈕事件
-(void)textButtonAction{
self.chooseAlignmentView.hidden=NO;
}
//選擇文本對齊方式view
-(UIView *)chooseAlignmentView{
if (_chooseAlignmentView==nil) {
_chooseAlignmentView=[[UIView alloc]initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width-145, self.textButton.frame.origin.y, 170, 50)];
_chooseAlignmentView.layer.cornerRadius=25;
_chooseAlignmentView.layer.masksToBounds=YES;
_chooseAlignmentView.layer.borderColor=[[UIColor whiteColor] CGColor];
_chooseAlignmentView.layer.borderWidth=2;
_chooseAlignmentView.backgroundColor=[UIColor colorWithRed:253/255.0 green:163/255.0 blue:155/255.0 alpha:1];
//創(chuàng)建文本左對齊textview
UIButton *leftButton=[UIButton buttonWithType:UIButtonTypeCustom];
leftButton.backgroundColor=[UIColor colorWithRed:253/255.0 green:163/255.0 blue:155/255.0 alpha:1];
[leftButton setImage:[UIImage imageNamed:@"left"] forState:UIControlStateNormal];
leftButton.layer.cornerRadius=20;
leftButton.layer.masksToBounds=YES;
leftButton.tag=1000;
[leftButton addTarget:self action:@selector(alignButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//創(chuàng)建文本居中對齊的textview
UIButton *centerButton=[UIButton buttonWithType:UIButtonTypeCustom];
centerButton.layer.cornerRadius=20;
centerButton.layer.masksToBounds=YES;
centerButton.backgroundColor=[UIColor colorWithRed:253/255.0 green:163/255.0 blue:155/255.0 alpha:1];
[centerButton setImage:[UIImage imageNamed:@"center"] forState:UIControlStateNormal];
centerButton.tag=2000;
[centerButton addTarget:self action:@selector(alignButtonAction:) forControlEvents:UIControlEventTouchUpInside];
//取消創(chuàng)建textview
UIButton *cancelButton=[UIButton buttonWithType:UIButtonTypeCustom];
cancelButton.layer.cornerRadius=20;
cancelButton.layer.masksToBounds=YES;
cancelButton.backgroundColor=[UIColor colorWithRed:253/255.0 green:163/255.0 blue:155/255.0 alpha:1];
[cancelButton setImage:[UIImage imageNamed:@"cancel1"] forState:UIControlStateNormal];
cancelButton.tag=3000;
[cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
leftButton.frame=CGRectMake(25, 5, 40, 40);
[_chooseAlignmentView addSubview:leftButton];
centerButton.frame=CGRectMake(65, 5, 40, 40);
[_chooseAlignmentView addSubview:centerButton];
cancelButton.frame=CGRectMake(105, 5, 40, 40);
[_chooseAlignmentView addSubview:cancelButton];
_chooseAlignmentView.hidden=YES;
}
return _chooseAlignmentView;
}
-(void)cancelButtonAction{
self.chooseAlignmentView.hidden=YES;
}
-(void)alignButtonAction:(UIButton *)button{
UITextView * textview=[UITextView new];
textview.layer.borderColor=[UIColor redColor].CGColor;
textview.layer.borderWidth=2;
textview.delegate=self;
textview.text=@"";
textview.backgroundColor=[UIColor clearColor];
textview.font=[UIFont fontWithName:@"FZJLJW--GB1-0" size:26];
self.toolBarView.frame=CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
textview.inputAccessoryView=self.toolBarView;
//給textview添加長按手勢
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longTap:)];
[textview addGestureRecognizer:longPress];
//給textview添加拖拽手勢
UIPanGestureRecognizer *ges=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
[textview addGestureRecognizer:ges];
if (self.totalViewsCount==0) {
//如果創(chuàng)建的該textview是創(chuàng)建的第一個控件的布局
textview.frame=CGRectMake(8, 8, [UIScreen mainScreen].bounds.size.width-16, 80);
if (button.tag==1000) {
textview.textAlignment=NSTextAlignmentLeft;
}else{
textview.textAlignment=NSTextAlignmentCenter;
}
}else{
//如果創(chuàng)建的該textview不是第一個控件的布局大磺,lastview記錄的是該頁面的最后一個控件
textview.frame=CGRectMake(8, self.lastView.frame.origin.y+self.lastView.frame.size.height+8, [UIScreen mainScreen].bounds.size.width-16, 80);
if (button.tag==1000) {
textview.textAlignment=NSTextAlignmentLeft;
}else{
textview.textAlignment=NSTextAlignmentCenter;
}
}
[self cellAddSubview:textview];
self.chooseAlignmentView.hidden=YES;
}
-(void)cellAddSubview:(UIView *)view{
UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
[self.totalViewArr addObject:view];
self.lastView=view;
self.totalViewsCount++;
view.tag=930+self.totalViewArr.count;
[cell.contentView addSubview:view];
self.cellHeight=self.lastView.frame.origin.y+self.lastView.frame.size.height+13;
[self.tableview reloadData];
}
//長按textview選擇是否刪除
-(void)longTap:(UITapGestureRecognizer *)ges{
UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:@"確定刪除嗎" message:nil preferredStyle:UIAlertControllerStyleAlert];
[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[alertVC addAction:[UIAlertAction actionWithTitle:@"確定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.changedTextview=(UITextView *)ges.view;
//從self.totalViewArr中刪除需要刪除的textview
NSMutableArray *tmp=[NSMutableArray array];
for (UIView *view in self.totalViewArr) {
if (view.tag==self.changedTextview.tag) {
}else{
[tmp addObject:view];
}
}
self.totalViewArr=[NSMutableArray arrayWithArray:tmp];
//給self.totalViewArr中的控件重新賦tag值
for (UIView *view in self.totalViewArr) {
NSInteger index=[self.totalViewArr indexOfObject:view];
view.tag=931+index;
}
[self reLayoutSubviewsInCell:self.changedTextview];
}]];
[self presentViewController:alertVC animated:YES completion:^{
}];
}
//重新布局tableviewcell中的子控件
-(void)reLayoutSubviewsInCell:(UIView *)changedView{
UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
NSArray *views=cell.contentView.subviews;
for (int i=0; i<views.count; i++) {
UIView *view=views[i];
if (i==changedView.tag-931) {
[view removeFromSuperview];
}
}
for (int i=0; i<self.totalViewArr.count; i++) {
UIView *view=self.totalViewArr[i];
if (i<changedView.tag-931) {
}else{
CGRect rect=view.frame;
view.frame=CGRectMake(rect.origin.x, rect.origin.y-changedView.frame.size.height-8, rect.size.width, rect.size.height);
}
}
self.lastView=[self.totalViewArr lastObject];
self.cellHeight=self.lastView.frame.origin.y+self.lastView.frame.size.height+13;
[self.tableview reloadData];
}
//textview的拖拽手勢
-(void)panAction:(UIPanGestureRecognizer *)ges{
UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
UIView *gesview=ges.view;
CGPoint point=[ges translationInView:cell.contentView];
if (ges.state==UIGestureRecognizerStateEnded) {
if (point.y>=0) {
NSMutableArray *tmp=[NSMutableArray array];
//往下
for (int i=(int)gesview.tag-930; i<self.totalViewArr.count; i++) {
UIView *view=self.totalViewArr[i];
if (i==self.totalViewArr.count-1) {
if (gesview.center.y+point.y>view.center.y ) {
//ok
if (self.totalViewArr.count==2) {
UIView *last=[self.totalViewArr lastObject];
CGRect rect=last.frame;
last.frame=CGRectMake(rect.origin.x,gesview.frame.origin.y ,rect.size.width , rect.size.height);
gesview.frame=CGRectMake(gesview.frame.origin.x, last.frame.size.height+last.frame.origin.y+8, gesview.frame.size.width, gesview.frame.size.height);
[tmp addObject:last];
[tmp addObject:gesview];
[self breakFor:tmp];
break;
}else{
//ok
if (gesview.tag==931) {
for (int j=1; j<self.totalViewArr.count; j++) {
UIView *view=self.totalViewArr[j];
CGRect rect=view.frame;
view.frame=CGRectMake(rect.origin.x, rect.origin.y-8-gesview.frame.size.height, rect.size.width, rect.size.height);
[tmp addObject:view];
}
UIView *last=tmp[self.totalViewArr.count-2];
gesview.frame=CGRectMake(gesview.frame.origin.x, last.frame.size.height+8+last.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
[tmp addObject:gesview];
[self breakFor:tmp];
break;
//ok
}else{
for (int j=0; j<gesview.tag-931; j++) {
UIView *view=self.totalViewArr[j];
[tmp addObject:view];
}
for (int j=(int)gesview.tag-930; j<self.totalViewArr.count; j++) {
UIView *view=self.totalViewArr[j];
CGRect rect=view.frame;
view.frame=CGRectMake(rect.origin.x, rect.origin.y-8-gesview.frame.size.height, rect.size.width, rect.size.height);
[tmp addObject:view];
}
UIView *last=[tmp lastObject];
gesview.frame=CGRectMake(gesview.frame.origin.x, last.frame.origin.y+last.frame.size.height+8, gesview.frame.size.width, gesview.frame.size.height);
[tmp addObject:gesview];
[self breakFor:tmp];
break;
}
}
}
}else{
UIView *nextView=self.totalViewArr[i+1];
if (gesview.center.y+point.y>view.center.y && gesview.center.y+point.y<nextView.center.y) {
//ok
if (gesview.tag==931) {
for (int j=1; j<i+1; j++) {
UIView *view=self.totalViewArr[j];
CGRect rect=view.frame;
view.frame=CGRectMake(rect.origin.x, rect.origin.y-8-gesview.frame.size.height, rect.size.width, rect.size.height);
[tmp addObject:view];
}
UIView *pre=self.totalViewArr[i];
gesview.frame=CGRectMake(gesview.frame.origin.x, pre.frame.origin.y+pre.frame.size.height+8, gesview.frame.size.width, gesview.frame.size.height);
[tmp addObject:gesview];
for (int j=i+1; j<self.totalViewArr.count; j++) {
UIView *view=self.totalViewArr[j];
[tmp addObject:view];
}
[self breakFor:tmp];
break;
//ok
}else{
for (int j=0; j<gesview.tag-931; j++) {
[tmp addObject:self.totalViewArr[j]];
}
for (int j=(int)gesview.tag-930; j<i+1; j++) {
UIView *tmpview=self.totalViewArr[j];
CGRect rect=tmpview.frame;
tmpview.frame=CGRectMake(rect.origin.x, rect.origin.y-8-gesview.frame.size.height, rect.size.width, rect.size.height);
[tmp addObject:tmpview];
}
UIView *last=[tmp lastObject];
gesview.frame=CGRectMake(gesview.frame.origin.x, last.frame.origin.y+last.frame.size.height+8, gesview.frame.size.width, gesview.frame.size.height);
[tmp addObject:gesview];
for (int j=i+1; j<self.totalViewArr.count; j++) {
[tmp addObject:self.totalViewArr[j]];
}
[self breakFor:tmp];
break;
}
}
}
}
}else{
NSMutableArray *tmp=[NSMutableArray array];
for (int i=0; i<gesview.tag-931; i++) {
UIView *view=self.totalViewArr[i];
if (gesview.center.y+point.y<view.center.y) {
if (self.totalViewArr.count==2) {
gesview.frame=CGRectMake(gesview.frame.origin.x, view.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
[tmp addObject:gesview];
view.frame=CGRectMake(view.frame.origin.x, gesview.frame.origin.y+gesview.frame.size.height+8, view.frame.size.width, view.frame.size.height);
[tmp addObject:view];
[self breakFor:tmp];
break;
}else{
if (view.tag==931&&gesview.tag==930+self.totalViewArr.count) {
gesview.frame=CGRectMake(gesview.frame.origin.x, view.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
[tmp addObject:gesview];
for (int j=0; j<self.totalViewArr.count-1; j++) {
UIView *viewtmp=self.totalViewArr[j];
viewtmp.frame=CGRectMake(viewtmp.frame.origin.x, viewtmp.frame.origin.y+gesview.frame.size.height+8, viewtmp.frame.size.width, viewtmp.frame.size.height);
[tmp addObject:viewtmp];
}
[self breakFor:tmp];
break;
}else if (gesview.tag==930+self.totalViewArr.count && view.tag!=931){
for (int j=0; j<view.tag-931; j++) {
UIView *tmpview=self.totalViewArr[j];
[tmp addObject:tmpview];
}
gesview.frame=CGRectMake(gesview.frame.origin.x, view.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
[tmp addObject:gesview];
for (int j=(int)view.tag-931; j<self.totalViewArr.count-1; j++) {
UIView *tmpview=self.totalViewArr[j];
CGRect rect=tmpview.frame;
tmpview.frame=CGRectMake(rect.origin.x,gesview.frame.size.height+8+rect.origin.y, rect.size.width, rect.size.height);
[tmp addObject:tmpview];
}
[self breakFor:tmp];
break;
}else if (view.tag==931 && gesview.tag!=self.totalViewArr.count+930){
gesview.frame=CGRectMake(gesview.frame.origin.x, view.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
[tmp addObject:gesview];
for (int j=0; j<gesview.tag-931; j++) {
UIView *tmpview=self.totalViewArr[j];
CGRect rect=tmpview.frame;
tmpview.frame=CGRectMake(rect.origin.x, rect.origin.y+gesview.frame.size.height+8, rect.size.width, rect.size.height);
[tmp addObject:tmpview];
}
for (int j=(int)gesview.tag-930; j<self.totalViewArr.count; j++) {
UIView *tmpview=self.totalViewArr[j];
[tmp addObject:tmpview];
}
[self breakFor:tmp];
break;
}else if (view.tag!=931 && gesview.tag!=self.totalViewArr.count+930){
for (int j=0; j<i; j++) {
UIView *tmpview=self.totalViewArr[j];
[tmp addObject:tmpview];
}
gesview.frame=CGRectMake(gesview.frame.origin.x, view.frame.origin.y, gesview.frame.size.width, gesview.frame.size.height);
[tmp addObject:gesview];
for (int j=i; j<gesview.tag-931; j++) {
UIView *tmpview=self.totalViewArr[j];
CGRect rect=tmpview.frame;
tmpview.frame=CGRectMake(rect.origin.x,rect.origin.y+gesview.frame.size.height+8 , rect.size.width, rect.size.height);
[tmp addObject:tmpview];
}
for (int j=(int)gesview.tag-930; j<self.totalViewArr.count; j++) {
UIView *tmpview=self.totalViewArr[j];
[tmp addObject:tmpview];
}
[self breakFor:tmp];
break;
}
}
}
}
}
}
}
-(void)breakFor:(NSMutableArray *)tmp{
UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
self.totalViewArr=[NSMutableArray arrayWithArray:tmp];
for (UIView *view in self.totalViewArr) {
[view removeFromSuperview];
NSInteger index=[self.totalViewArr indexOfObject:view];
view.tag=931+index;
}
self.lastView=[self.totalViewArr lastObject];
for (UIView *view in self.totalViewArr) {
[cell.contentView addSubview:view];
}
[self.tableview reloadData];
}
imageview相關
-(UIButton *)pictureButton{
if (_pictureButton==nil) {
_pictureButton=[UIButton buttonWithType:UIButtonTypeCustom];
[_pictureButton setImage:[UIImage imageNamed:@"picture"] forState:UIControlStateNormal];
_pictureButton.layer.cornerRadius=25;
_pictureButton.layer.masksToBounds=YES;
_pictureButton.backgroundColor=[UIColor whiteColor];
[_pictureButton addTarget:self action:@selector(pictureButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _pictureButton;
}
//點擊圖片按鈕選擇拍照還是相冊
-(void)pictureButtonAction{
UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertVC addAction:[UIAlertAction actionWithTitle:@"相冊" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *vc=[[UIImagePickerController alloc]init];
vc.delegate=self;
vc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
vc.allowsEditing=YES;
vc.videoQuality=UIImagePickerControllerQualityTypeHigh;
[self presentViewController:vc animated:YES completion:^{
}];
}]];
[alertVC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImagePickerController *vc=[[UIImagePickerController alloc]init];
vc.delegate=self;
vc.sourceType=UIImagePickerControllerSourceTypeCamera;
vc.allowsEditing=YES;
vc.videoQuality=UIImagePickerControllerQualityTypeHigh;
[self presentViewController:vc animated:YES completion:^{
}];
}]];
[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertVC animated:YES completion:^{
}];
}
//圖片選擇完成后的操作
-(void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary )info{
NSLog(@"%@",info);
UIImage *img=[info objectForKey:@"UIImagePickerControllerEditedImage"];
//UIImage *img=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
CGSize size=img.size;
if (self.createOrNot==YES) {
UIImageView * imageview1=[UIImageView new];
imageview1.layer.cornerRadius=10;
imageview1.layer.masksToBounds=YES;
imageview1.layer.borderColor=[UIColor whiteColor].CGColor;
imageview1.layer.borderWidth=6;
imageview1.userInteractionEnabled=YES;
imageview1.image=img;
UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(change:)];
[imageview1 addGestureRecognizer:tap];
if (self.totalViewsCount==0) {
if (size.width>[UIScreen mainScreen].bounds.size.width) {
imageview1.frame=CGRectMake(6, 8, [UIScreen mainScreen].bounds.size.width-12, size.height*([UIScreen mainScreen].bounds.size.width-12)/size.width);
}else{
imageview1.frame=CGRectMake([UIScreen mainScreen].bounds.size.width/2.0-size.width/2.0, 8,size.width, size.height);
}
}else{
if (size.width>[UIScreen mainScreen].bounds.size.width) {
imageview1.frame=CGRectMake(6, self.lastView.frame.origin.y+self.lastView.frame.size.height+8, [UIScreen mainScreen].bounds.size.width-12, size.height*([UIScreen mainScreen].bounds.size.width-12)/size.width);
}else{
imageview1.frame=CGRectMake([UIScreen mainScreen].bounds.size.width/2.0-size.width/2.0, self.lastView.frame.origin.y+self.lastView.frame.size.height+8,size.width, size.height);
}
}
[self cellAddSubview:imageview1];
}else{
self.needChangedImageview.image=img;
self.createOrNot=YES;
}
[picker dismissViewControllerAnimated:YES completion:^{
}];
}
//點擊圖片進行修改或刪除
-(void)change:(UITapGestureRecognizer *)ges{
UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertVC addAction:[UIAlertAction actionWithTitle:@"改變圖片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
self.createOrNot=NO;
self.needChangedImageview=(UIImageView *)ges.view;
[self pictureButtonAction];
}]];
[alertVC addAction:[UIAlertAction actionWithTitle:@"刪除圖片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
UIImageView *imageview=(UIImageView *)ges.view;
self.changedImageView=imageview;
NSMutableArray *tmp=[NSMutableArray array];
for (UIView *view in self.totalViewArr) {
if (view.tag==imageview.tag) {
}else{
[tmp addObject:view];
}
}
self.totalViewArr=[NSMutableArray arrayWithArray:tmp];
for (UIView *view in self.totalViewArr) {
NSInteger index=[self.totalViewArr indexOfObject:view];
view.tag=931+index;
}
[self reLayoutSubviewsInCell:self.changedImageView];
}]];
[alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertVC animated:YES completion:^{
}];
}
tableview相關
-(UITableView *)tableview{
if (_tableview==nil) {
_tableview=[[UITableView alloc]init];
_tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
_tableview.delegate=self;
_tableview.dataSource=self;
}
return _tableview;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell )tableView:(UITableView )tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellid=@"cellid";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
if (cell==nil) {
cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
cell.backgroundColor=[UIColor colorWithRed:arc4random()%6/255.0 green:arc4random()%6/255.0 blue:arc4random()%6/255.0 alpha:1];
self.tableview.backgroundColor=cell.backgroundColor;
}
return cell;
}
-(CGFloat)tableView:(UITableView )tableView heightForRowAtIndexPath:(NSIndexPath )indexPath{
return self.cellHeight;
}
隨機改變背景顏色
-(UIButton *)colorButton{
if (_colorButton==nil) {
_colorButton=[UIButton buttonWithType:UIButtonTypeCustom];
_colorButton.backgroundColor=[UIColor whiteColor];
[_colorButton setImage:[UIImage imageNamed:@"color"] forState:UIControlStateNormal];
_colorButton.layer.cornerRadius=25;
_colorButton.layer.masksToBounds=YES;
[_colorButton addTarget:self action:@selector(colorButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _colorButton;
}
//隨機改變背景顏色
-(void)colorButtonAction{
UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
cell.backgroundColor=[UIColor colorWithRed:arc4random()%6/255.0 green:arc4random()%6/255.0 blue:arc4random()%6/255.0 alpha:1];
self.tableview.backgroundColor=cell.backgroundColor;
}
保存圖片到手機
-(UIButton *)saveButton{
if (_saveButton==nil) {
_saveButton=[UIButton buttonWithType:UIButtonTypeCustom];
_saveButton.backgroundColor=[UIColor whiteColor];
[_saveButton setImage:[UIImage imageNamed:@"finish"] forState:UIControlStateNormal];
_saveButton.layer.cornerRadius=25;
_saveButton.layer.masksToBounds=YES;
[_saveButton addTarget:self action:@selector(saveButtonAction) forControlEvents:UIControlEventTouchUpInside];
}
return _saveButton;
}
-(void)saveButtonAction{
UITableViewCell *cell=[self.tableview cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
for (UIView *view in [cell.contentView subviews]) {
if ([view isKindOfClass:[UITextView class]]) {
UITextView *textview=(UITextView *)view;
textview.layer.borderWidth=0;
}
}
UIImageWriteToSavedPhotosAlbum([self convertViewToImage:cell], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
//view轉為image
-(UIImage )convertViewToImage:(UIView )v{
CGSize s=v.bounds.size;
UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);
[v.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
//保存結束后的回調
-(void)image:(UIImage )image didFinishSavingWithError:(NSError )error contextInfo:(void *)contextInfo{
if(error == nil) {
NSLog(@"success");
}else{
NSLog(@"fail");
}
}
好了 ,差不多就是這些了探膊,歡迎指教杠愧。