需要实现的功能是:点击最上面的添加,增加一行,是从右面有一个动画效果,缓缓进入,其实每一行就是一个新的view,点击垃圾箱时把最后一行删除。然后再在在个view上添加两个button,一个背景是笑脸,另一个就是删除,中间的文字就是联系人的姓名,点击笑脸button时,显示联系人姓名,点击删除button则删除当前行,并且后面的行向上补齐,并且有一个动画效果。
为了简便,用了.xib。
心得:最核心的就是每一个view的y坐标的算法。可以利用subviews这个属性,当然在点击删除按钮删除当前行时,也需要用到superview。找到button所在的view。
以下为ViewController.m里面的程序
//
// ViewController.m
// 联系人列表1
//
// Created by jerei on 14-12-27.
// Copyright (c) 2014年 jerei. All rights reserved.
//
#import "ViewController.h"
#define VIEW_H 60
@interface ViewController ()
{
@private
NSArray *_nameAry;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
_nameAry = @[@"aaa",@"bbb",@"ccc",@"ddd",@"eee",@"fff"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark 删除最后一行
- (IBAction)removeView:(UIBarButtonItem *)sender {
UIView *lastView = [self.view.subviews lastObject];
//判断取到的最后一个view是不是工具条,如果是,返回
Class tempClass = [UIToolbar class];
if ([lastView isKindOfClass:tempClass])
return;
//动画
//判断是不是全部删除完毕
[UIView animateWithDuration:0.5 animations:^{
CGRect frameD = lastView.frame;
frameD.origin.x = 320;
lastView.frame = frameD;
lastView.opaque = 0;
} completion:^(BOOL finished) {
//把最后一个view删除
[lastView removeFromSuperview];
_removeBtn.enabled = self.view.subviews.count>1;
}];
}
#pragma mark 创建新的一行的view
-(UIView *)newRowView
{
//创建uiview
NSArray *rowAry = [[NSBundle mainBundle] loadNibNamed:@"RowView" owner:self options:nil];
UIView *rowView = rowAry[0];
//添加表情button
UIButton *btn = (UIButton *)rowView.subviews[0];
[btn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"face%i.png",arc4random()%9+1]] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(clickIconBtn:) forControlEvents:UIControlEventTouchUpInside];
//添加label
UILabel *label = (UILabel *)rowView.subviews[1];
[label setText:[NSString stringWithFormat:@"%@",_nameAry[arc4random()%_nameAry.count]]];
//添加删除button
UIButton *btn1 = (UIButton *)rowView.subviews[2];
[btn1 addTarget:self action:@selector(clickDeleBtn:) forControlEvents:UIControlEventTouchUpInside];
return rowView;
}
#pragma mark 点击头像按钮
-(void)clickIconBtn:(UIButton*)btn
{
UILabel *lab = btn.superview.subviews[1];
NSLog(@"%@",lab.text);
}
#pragma mark 点击每行的删除按钮
-(void)clickDeleBtn:(UIButton*)btn
{
UIView *nowView = btn.superview;
[UIView animateWithDuration:0.5 animations:^{
//view出去的动画
CGRect frameD = nowView.frame;
frameD.origin.x = 320;
nowView.frame = frameD;
nowView.opaque = 0;
} completion:^(BOOL finished) {
//下面的行上移
NSArray *ary = self.view.subviews;
int index = [ary indexOfObject:nowView];
[UIView animateWithDuration:0.2 animations:^{
for (int i=index; i<ary.count; i++)
{
UIView *view1 = (UIView*)ary[i];
CGRect frame1 = view1.frame;
frame1.origin.y -= (VIEW_H+2);
view1.frame = frame1;
}
}];
//删除当前view
[nowView removeFromSuperview];
_removeBtn.enabled = self.view.subviews.count>1;
}];
}
#pragma mark 添加一行
- (IBAction)addView:(UIBarButtonItem *)sender {
//创建View
UIView *rowView = [self newRowView];
UIView *lastView = [self.view.subviews lastObject];
rowView.alpha = 0;
rowView.frame = CGRectMake(320, lastView.frame.origin.y+lastView.frame.size.height+2, 320, VIEW_H);
[self.view addSubview:rowView];
//view进来的动画
[UIView animateWithDuration:0.5 animations:^{
CGRect rowFrame = rowView.frame;
rowFrame.origin.x = 0;
rowView.frame = rowFrame;
rowView.alpha = 1;
//点亮删除按钮
_removeBtn.enabled = YES;
} completion:^(BOOL finished) {
NSLog(@"完成添加的动画");
}];
}
@end