簡體   English   中英

如何用單指旋轉圖像視圖並調整其大小

[英]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);

這可能會有所幫助, https://github.com/zedoul/ZDStickerView

IQStickerView具有OneFingerRotation,Scale,Resize和Close功能。

特征:

  1. 單指旋轉量表。
  2. 一根手指調整大小。
  3. 啟用/可用旋轉,縮放,調整屬性大小。
  4. 自動管理多個IQStickerView。
  5. 也可以使用UIScrollView。
  6. 快速響應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM