程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 拖動實現圖片移動效果

拖動實現圖片移動效果

編輯:C++入門知識

拖動實現圖片移動效果


拖動實現圖片移動效果

 

先寫一個手勢,注意圖片的 userInteractionEnabled設置為yes

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];

UIImage *image = [UIImage imageNamed:@r.jpg];

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 40, 80, 80)];

imageView.image = image;

imageView.userInteractionEnabled = YES;

[self addSubview:imageView];

[imageView addGestureRecognizer:pan];

 

 

}

拖動的方法,最後一句是關鍵代碼

 

- (void)pan:(UIPanGestureRecognizer *)gesture

{

CGPoint point = [gesture translationInView:self];

gesture.view.center = CGPointMake(gesture.view.center.x + point.x, gesture.view.center.y + point.y);

[gesture setTranslation:CGPointMake(0, 0) inView:self];

 

}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved