码迷,mamicode.com
首页 > 其他好文 > 详细

手势拖动. 约束图片边缘 .限制拉动范围

时间:2017-08-10 13:38:56      阅读:163      评论:0      收藏:0      [点我收藏+]

标签:图片   change   nts   selector   sla   setup   ini   style   size   

 

 

@implementation WDEnterLineViewController{
    
    float xx;
    float yy;
    UIImageView *bgImgView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createLayout];
    
}


-(void)createLayout{
    
    bgImgView = [[UIImageView alloc]init];
    
    UIImage *image = [UIImage imageNamed:@"110"];
    bgImgView.image = image;
    CGImageRef imageRef = [image CGImage];
    CGSize size = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
    xx = size.width;
    yy = size.height;
    WDLog(@"%f====%f",xx,yy);
    [self.view addSubview:bgImgView];
    
    [bgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY);
        make.width.offset(size.width);
        make.height.offset(size.height);
        
    }];
    
    
    [self addGestureRecognizerToView:bgImgView];
    
    //如果处理的是图片.
    [bgImgView setUserInteractionEnabled:YES];
    [bgImgView setMultipleTouchEnabled:YES];
    
}

// 添加手势
- (void) addGestureRecognizerToView:(UIView *)view
{
    // 移动手势
    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panView:)];
    [view addGestureRecognizer:panGestureRecognizer];
}


// 处理拖拉手势
- (void) panView:(UIPanGestureRecognizer *)panGestureRecognizer
{
    UIView *view = panGestureRecognizer.view;
    if (panGestureRecognizer.state == UIGestureRecognizerStateBegan || panGestureRecognizer.state == UIGestureRecognizerStateChanged) {
        CGPoint translation = [panGestureRecognizer translationInView:view.superview];
        [view setCenter:(CGPoint){view.center.x + translation.x, view.center.y + translation.y}];
        [panGestureRecognizer setTranslation:CGPointZero inView:view.superview];
    }else if(panGestureRecognizer.state == UIGestureRecognizerStateEnded){
        WDLog(@"-centerX:%f===centerY:%f",bgImgView.center.x,bgImgView.center.y);
        WDLog(@"%f====%f",self.view.center.x,self.view.center.y)
        
        CGPoint point = CGPointMake(bgImgView.center.x, bgImgView.center.y);
        
        
        if (point.x >= xx / 2) {
            point.x = xx / 2;
        };
        if (point.x <= self.view.center.x - (xx - APP_WIDTH) / 2) {
            point.x = self.view.center.x - (xx - APP_WIDTH) / 2;
        };
        if (point.y >= yy / 2 + 64) {
            point.y = yy / 2 + 64;
        };
        if (point.y <= self.view.center.y - (yy - APP_HEIGHT) / 2) {
            point.y = self.view.center.y - (yy - APP_HEIGHT) / 2;
        };
        
        bgImgView.center = point;
    }
}

 

手势拖动. 约束图片边缘 .限制拉动范围

标签:图片   change   nts   selector   sla   setup   ini   style   size   

原文地址:http://www.cnblogs.com/-yun/p/7338439.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!