iOS开发之实现文件夹内文件拖出效果

用掌阅iReader看小说时注意到一个文件夹内文件的拖动效果,秉着对未知事物的好奇心态自己试着做了做类似效果_,类似下图

iOS开发之实现文件夹内文件拖出效果_第1张图片
拖动文件夹.png

1、原来效果是这样的

iReader.gif

2、我实现的效果是这样的

iOS开发之实现文件夹内文件拖出效果_第2张图片
WBViewDrag.gif

在这里我做了左右边界的限制,图块是不能超出左右屏幕滴!如果拖动结束,图块停留在上下边界我就让图块回到了原位置。

图块拖出灰色视图时灰色视图自动隐藏,不过我们可以将图块拖到底端唤出灰色视图。

3、实现很简单,废话不多说了直接上代码

@interface ViewController ()
{
    CGPoint startPoint;
    UIView *drag;
    UIView *dragBac;
}
@end
#define screenWidth self.view.frame.size.width
#define screenHeight self.view.frame.size.height
- (void)viewDidLoad {
    [super viewDidLoad];
    dragBac=[[UIView alloc]initWithFrame:CGRectMake(0, screenHeight/2, screenWidth, screenHeight/2)];
    dragBac.userInteractionEnabled=YES;
    dragBac.backgroundColor=[UIColor lightGrayColor];
    dragBac.layer.shadowColor = [UIColor blackColor].CGColor;//shadowColor阴影颜色
    dragBac.layer.shadowOffset = CGSizeMake(6,6);//shadowOffset阴影偏移,x向右偏移4,y向下偏移4,默认(0, -3),这个跟shadowRadius配合使用
    dragBac.layer.shadowOpacity = 0.8;//阴影透明度,默认0
    dragBac.layer.shadowRadius = 10;//阴影半径,默认3
    
    [self.view addSubview:dragBac];
    
    drag=[[UIView alloc]initWithFrame:CGRectMake(0, screenHeight/2, 50, 50)];
    drag.backgroundColor=[UIColor blackColor];
    [self.view addSubview:drag];
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //保存触摸起始点位置
    CGPoint point = [[touches anyObject] locationInView:self.view];
    startPoint = point;
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    CGPoint point = [[touches anyObject] locationInView:self.view];
    //判断是不是出了左右边界
    point.x=MIN(point.x, screenWidth-25);
    point.x=MAX(point.x, 25);
    
    CGPoint newcenter = CGPointMake(point.x, point.y);
    //移动view
    drag.center = newcenter;
    
    if ((point.y+25)screenHeight) {
        
        [UIView animateWithDuration:0.5 animations:^{
            dragBac.alpha=1;
            
        }];
    } 
}
- (void)touchesEnded:(NSSet *)touches withEvent:(nullable UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self.view];
    
    //未全部拖出,返回原位置
    if (((point.y+25)>screenHeight/2&&(point.y-25)screenHeight&&(point.y-25)

你可能感兴趣的:(iOS开发之实现文件夹内文件拖出效果)