[英]How to rotate UIButton or UIImage view that follows finger (Touch and hold)?
[英]How to rotate and resize the image view with single finger
我正在開發一個應用程序,其功能是通過拖動其右下角按鈕來調整和旋轉imageview。
我看到一個應用程序的功能,如果我們拖動右下角按鈕對角線imageview大小已調整大小,否則如果我們拖動按鈕左側或右側方向imageview已按方向旋轉。 我希望在我的應用中實現此功能
我正在努力實現單指旋轉以及調整imageview的大小。
我可以通過拖動它的右下角按鈕成功實現調整imageview的大小。 但我沒有足夠的知識來向圖像視圖添加旋轉
請以正確的方式指導我。
我已添加以下代碼,通過拖動其右上角來調整圖像視圖的大小。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
touchStart = [[touches anyObject] locationInView:imageView];
isResizingLR = (containerVw.bounds.size.width - touchStart.x < kResizeThumbSize && containerVw.bounds.size.height - touchStart.y < kResizeThumbSize);
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
CGPoint touchPoint = [[touches anyObject] locationInView:imageView];
CGPoint previous=[[touches anyObject]previousLocationInView:imageView];
UITouch *touch = [[event allTouches] anyObject];
float deltaWidth = touchPoint.x-previous.x;
float deltaHeight = touchPoint.y-previous.y;
if (isResizingLR) {
containerVw.frame = CGRectMake(containerVw.frame.origin.x, containerVw.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
imageView.frame = CGRectMake(imageView.frame.origin.x, imageView.frame.origin.y,touchPoint.x + deltaWidth, touchPoint.y + deltaWidth);
dragIm.frame = CGRectMake(containerVw.frame.size.width-10, containerVw.frame.size.height-10,20,20);
if (!isResizingLR) {
containerVw.center = CGPointMake(containerVw.center.x + touchPoint.x touchStart.x,containerVw.center.y + touchPoint.y - touchStart.y);
}
}
我遇到了與你相同的障礙,所以我開發了自己的模塊ZDStickerView 。 這將是一個很好的參考。
首先,確保您的視圖的autoresizingMask應該是靈活的。
autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
否則調整大小將無法正常工作。
其次,我建議你使用“CGAffineTransformMakeRotation”和“atan2”函數來解決旋轉問題,如下所示:
float ang = atan2([recognizer locationInView:self.superview].y - self.center.y,
[recognizer locationInView:self.superview].x - self.center.x);
float angleDiff = deltaAngle - ang;
self.transform = CGAffineTransformMakeRotation(-angleDiff);
第三,一定要使用相對坐標,如下所示:
self.transform = CGAffineTransformMakeRotation(-angleDiff);
我已修復此問題並將此控件上傳到cocoacontrols
http://www.cocoacontrols.com/controls/tdresizerview
和示例代碼
https://www.dropbox.com/s/yb0vzy0wao52bgt/SampeResizeView.zip
更新的答案
這可能會有所幫助, https://github.com/zedoul/ZDStickerView 。
IQStickerView具有OneFingerRotation,Scale,Resize和Close功能。
特征:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.