2016-09-07 105 views
0

我正在實現一個圖像裁剪器,它在圖像頂部有一個圓形區域。圓內的圖像區域必須裁剪。現在,我必須爲裁剪視圖提供縮放功能。但我的要求是,當用戶觸摸UiView的邊框並平移時,我必須縮放裁剪視圖。所以我不能使用捏合手勢。我附上了一張圖片,以便清楚瞭解我的要求。任何人都可以提供相同的優化解決方案。檢測UIView邊框上的觸摸

enter image description here

+0

如果它是一個完美的圓圈,並且由於您知道觸摸位置,使用簡單的數學運算就可以輕鬆知道您是否在允許的區域內。 – Larme

+0

你可以從這個https://github.com/kishikawakatsumi/PEPhotoCropEditor得到一個想法 – Jeyamahesan

+0

兄弟sree檢查我的答案。我現在更新。 – user3182143

回答

1

我已經找到了解決,以確定一個圓的邊緣上的觸摸。基本上,我有一個UIView,我已經給出了UIView寬度一半的圓角半徑以獲得20px的圓形外觀和邊框厚度。

我有從圓邊框的外邊緣和內邊緣的圓的半徑,我可以從視圖框架和邊框厚度值得到。然後我使用方程radius^2 =(touchX-centerX)^ 2 +(touchY-centerY)^ 2,基於觸摸x,y和中心x,y計算半徑。如果計算的半徑以b/w爲內半徑和外半徑,則觸摸位於圓的邊緣。希望這會幫助某人。

+0

好兄弟以另一種方式尋找解決方案,所以我upvoted。 – user3182143

+0

指出方程式兄弟。 – user3182143

1

我試圖解決方案,我得到了它。

ViewController.h

#import <UIKit/UIKit.h> 
@interface ViewController : UIViewController 
@property (strong, nonatomic) IBOutlet UIView *viewCircleTouchPoint; 
@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 
{ 
    float xValue,yValue; 
} 
@end 
@implementation ViewController 
@synthesize viewCircleTouchPoint; 

現在你可以使用兩個選項

第一個選項:觸摸事件方法

根據我的時候我觸摸了

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 

    UIGraphicsBeginImageContext(viewCircleTouchPoint.frame.size); 
    [[UIImage imageNamed:@"txNTO.png"] drawInRect:viewCircleTouchPoint.bounds]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    viewCircleTouchPoint.backgroundColor = [UIColor colorWithPatternImage:image]; 
} 

根據我當我觸摸視圖的箭頭部分時,我得到的x值爲168,y值爲50。所以如果它小於x和y值,它將不允許pan.If它是大於或等於我設置視圖。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch =[[event allTouches]anyObject]; 
    CGPoint touchPoint = [touch locationInView:imageCirclePoint]; 
    NSLog(@"The border touch point is - %@", NSStringFromCGPoint(touchPoint)); 
    NSLog(@"Touch x :%f y: :%f",touchPoint.x,touchPoint.y); 
    xValue =touchPoint.x; 
    yValue =touchPoint.y; 
    if(xValue>=168 && yValue>=50){ 
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; 
    [panRecognizer setMinimumNumberOfTouches:1]; 
    [panRecognizer setMaximumNumberOfTouches:1]; 
    [viewCircleTouchPoint addGestureRecognizer:panRecognizer]; 
    } 
} 

現在打印結果是

enter image description here

第二個選項:手勢reconizer

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 


    UIGraphicsBeginImageContext(viewCircleTouchPoint.frame.size); 
    [[UIImage imageNamed:@"txNTO.png"] drawInRect:viewCircleTouchPoint.bounds]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    viewCircleTouchPoint.backgroundColor = [UIColor colorWithPatternImage:image]; 


    UITapGestureRecognizer *tapgesture =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapDetected:)]; 
    tapgesture.numberOfTouchesRequired=1; 
    viewCircleTouchPoint.userInteractionEnabled = YES; 
    [viewCircleTouchPoint addGestureRecognizer:tapgesture]; 
} 

-(void)tapDetected:(UITapGestureRecognizer *)gesture 
{ 
    CGPoint point = [gesture locationInView:imageCirclePoint]; 
    NSLog(@"The border touch point is - %@", NSStringFromCGPoint(point)); 
    NSLog(@"Touch x :%f y: :%f",point.x,point.y); 
    if(point.x>=168 && point.y>=50){ 
    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panGesture:)]; 
    [panRecognizer setMinimumNumberOfTouches:1]; 
    [panRecognizer setMaximumNumberOfTouches:1]; 
    [viewCircleTouchPoint addGestureRecognizer:panRecognizer]; 
    } 
} 

-(void)panGesture:(UIPanGestureRecognizer *)gesturepan 
{ 
    NSLog(@"The pan gesture recognizer is called"); 
    .....//do your stuff here 
} 

印刷爲抽頭結果

enter image description here

看到我的iPhone嘗試截圖below.I沒有添加任何imageView查看。

enter image description here

+0

我不使用UIImageView的圓圈。我給出了圓形uiview的寬度爲25px的邊框。所以當用戶觸摸邊界區域時,平移手勢應該工作。所以我想要一個優化的機制來檢測UIView的圓形邊界區域上的觸摸。 –

+0

兄弟在代碼中給self.view代替imageCirclePoint。 – user3182143

+0

此外,您還需要在輕擊檢測方法中寫入平移手勢,在此方法中,如果觸摸點小於或等於,則必須將邊界的if條件置於其中。因此,如果相等,則在其內部寫入平移手勢代碼。 – user3182143