iOS 可移动View的实现

来源:互联网 发布:pdf打开软件 编辑:程序博客网 时间:2024/05/18 02:33

关于浮动view

#import "WMZRootViewController.h"


@interface WMZMySetViewController : WMZRootViewController


@property (nonatomic, assign) CGPoint beginPoint;


@end


  

    _aniImgView = [[UIImageView alloc] initWithFrame:CGRectMakeEx(200, 200, 64, 64)];

//    _aniImgView.backgroundColor = [UIColor redColor];

    _aniImgView.layer.cornerRadius = 32;

    _aniImgView.clipsToBounds = YES;

    _aniImgView.image = [UIImage imageNamed:@"qian"];

//    _aniImgView.alpha = 0.5;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(jifen)];

    [_aniImgView addGestureRecognizer:tap];

    _aniImgView.userInteractionEnabled = YES;

    [self.view addSubview:_aniImgView];

    _aniImgView.hidden = YES;



- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    CGPoint pt = [[touches anyObject] locationInView:_aniImgView];

    self.beginPoint = pt;

    

}

- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event

{

    // Calculate offset

    CGPoint pt = [[touches anyObject] locationInView:_aniImgView];

    float dx = pt.x - self.beginPoint.x;

    float dy = pt.y - self.beginPoint.y;

    CGPoint newcenter = CGPointMake(_aniImgView.center.x + dx,_aniImgView.center.y + dy);

    //可设置移动的区域

    // Set new location

    _aniImgView.center = newcenter;

}

0 0