IOS 实现类似桌面删除应用程序的效果(长按 可编辑 删除 图片按比例截取)

长平狐 发布于 2013/12/25 17:17
阅读 483
收藏 1

一个类似苹果桌面上长按删除app的效果,就是当你长按屏幕时,会出现缩小图片并出现删除按钮,点击删除按钮,删除图片并且后面图片前移。

当再次点击图片,恢复。点击图片有处理事件,可以在此做一些处理。

源码下载:http://download.csdn.net/download/rhljiayou/6282997

先看效果图:

   


  


实现思路&核心代码

继承于UIScrollView,先在上面创建出所有的view,在每个view上添加单击手势,在uiscrollView上添加长按手势。

//创建所有的view
- (void)createView
{
    x = 0;
    y = 0;
    width = self.frame.size.width /Column;
    for (int i = 0; i<_imageArray.count; i++) {
        x = (i%Column)*width;
        y = (i/Column)*(ImageHeight+Space);
        
        if (i%Column != 0) {
            x+=Space*(i%Column);
        }
        
        //bgView
        UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(x, y, width, ImageHeight)];        
        CALayer *imageLayer = bgView.layer;
        [imageLayer setMasksToBounds:YES];
        [imageLayer setCornerRadius:3];
        bgView.tag = i + BgViewTag;
        [self addSubview:bgView];
        
        //添加图像
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, bgView.frame.size.width, bgView.frame.size.height)];
        imageView.image = [Util imageBy:[UIImage imageNamed:[_imageArray objectAtIndex:i]] withWidth:imageView.frame.size.width*2 withHight:imageView.frame.size.height*2];
        [bgView addSubview:imageView];
        
        //添加删除按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(bgView.frame.size.width - 24, 0, 24, 24);
        [button setBackgroundImage:[UIImage imageNamed:@"delete"] forState:UIControlStateNormal];
        button.tag = BtnTag +i;
        [button addTarget:self action:@selector(buttonChange:) forControlEvents:UIControlEventTouchUpInside];
        [bgView addSubview:button];
        button.hidden = YES;
        
        //在bgview上添加点击手势事件
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer  alloc] initWithTarget:self action:@selector(tapOnContentView:)];
        [bgView addGestureRecognizer:tapGestureRecognizer];
        
        
    }
    //
    self.contentSize = CGSizeMake(self.frame.size.width, y+ImageHeight);
    //长按手势
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(LongPressGestureRecognizer:)];
    [self addGestureRecognizer:longGesture];
}


当你删除时,使其每个view上的删除按钮出现hidden=no,并view缩小

//是否编辑中
- (void)editView:(BOOL)isEdit
{
    _edit = isEdit;
    for (UIView *view in self.subviews)
    {
        for (UIView *v in view.subviews)
        {
            //所有的uibutton是否显示
            if ([v isMemberOfClass:[UIButton class]])
                [v setHidden:!isEdit];
        }
    }
    
    //缩小大小
    float scale;
    if (_edit) {
        scale = 0.9;
    }else{
        scale = 1.0;
    }
    
    for (UIView *view in self.subviews)
    {
        //动画缩小,放大
        [UIView animateWithDuration:0.1 delay:0.1 options:0  animations:^
         {
             view.transform=CGAffineTransformMakeScale(scale, scale);
         } completion:^(BOOL finished)
         {
             
         }];
    }
}


当你删除一个应用时,把UIScrollView上所有的view遍历一遍。把当前view删除,并使其 后面的view.frame==前面的view.frame,并动画移动后面的view。


//删除view时
-(void)buttonChange:(UIButton*)sender
{
    NSArray *views = self.subviews;
    __block CGRect newframe;
    int index = sender.tag - BtnTag;
    for (int i = index; i < [_imageArray count]; i++)
    {
        UIView *obj = [views objectAtIndex:i];
        __block CGRect nextframe = obj.frame;
        if (i == index)
        {
            //删除这个view
            [obj removeFromSuperview];
        }
        else
        {
            for (UIView *v in obj.subviews)
            {
                //把每个按钮的tag从重设置
                if ([v isMemberOfClass:[UIButton class]])
                {
                    v.tag = i+BtnTag - 1;
                    break;
                }
            }
            //并且位置动画改变
            [UIView animateWithDuration:0.6 animations:^
             {
                 obj.frame = newframe;
             } completion:^(BOOL finished)
             {
                 
             }];
        }
        //记住上一个view的位置 
        newframe = nextframe;
    }
    //数组移除 
    [_imageArray removeObjectAtIndex:index];

}

这个还是比较好实现的,你可以在这个基础上再添加一些新的功能,比如使长按view时来回移动,插入到某个view前面时,使期后面的view往后移,

说白了就是完全做成一个跟苹果删除app一样的效果实现,这个你可以尝试做一下!


在此声明,转载注明地址。




原文链接:http://blog.csdn.net/rhljiayou/article/details/11784821
加载中
返回顶部
顶部