检测 UIView 边界上的触摸
Detect the Touch on the borders of an UIView
我正在实现一个图像裁剪器,它在图像顶部有一个圆形区域。必须裁剪圆圈内的图像区域。现在我必须为裁剪视图提供缩放功能。但我的要求是,当用户触摸 UiView 的边界并平移时,我必须缩放裁剪视图。这样我就不能使用捏合手势。我附上了一张图片以清楚地了解我的要求。谁能给出一个优化的解决方案。
我尝试了解决方案,我明白了。
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;
现在您可以使用两个选项
第一个Option:touch事件方法
据我说当我触摸
- (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.So 如果它小于 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];
}
}
现在打印的结果是
第二个 Option:Gesture 侦察器
- (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
}
点击的打印结果
查看我的 iPhone 试过的屏幕截图 below.I 没有添加任何 imageView 以供查看。
我找到了一个解决方案来识别圆的边缘上的触摸。基本上,我有一个 UIView,我在其上给出了 UIView 宽度一半的角半径以获得圆形外观和 20px 的边框厚度。
我有从圆边框的外边缘和内边缘开始的圆半径,我可以从视图框架和边框厚度值中获得。然后,我使用等式 radius^2 = (touchX-centerX)^2 + (touchY-centerY)^2 根据触摸 x,y 和中心 x,y 计算了半径。如果计算出的半径在b/w的内径和外径之间,则触摸是在圆的边缘。希望这会对某人有所帮助。
我正在实现一个图像裁剪器,它在图像顶部有一个圆形区域。必须裁剪圆圈内的图像区域。现在我必须为裁剪视图提供缩放功能。但我的要求是,当用户触摸 UiView 的边界并平移时,我必须缩放裁剪视图。这样我就不能使用捏合手势。我附上了一张图片以清楚地了解我的要求。谁能给出一个优化的解决方案。
我尝试了解决方案,我明白了。
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;
现在您可以使用两个选项
第一个Option:touch事件方法
据我说当我触摸
- (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.So 如果它小于 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];
}
}
现在打印的结果是
第二个 Option:Gesture 侦察器
- (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
}
点击的打印结果
查看我的 iPhone 试过的屏幕截图 below.I 没有添加任何 imageView 以供查看。
我找到了一个解决方案来识别圆的边缘上的触摸。基本上,我有一个 UIView,我在其上给出了 UIView 宽度一半的角半径以获得圆形外观和 20px 的边框厚度。
我有从圆边框的外边缘和内边缘开始的圆半径,我可以从视图框架和边框厚度值中获得。然后,我使用等式 radius^2 = (touchX-centerX)^2 + (touchY-centerY)^2 根据触摸 x,y 和中心 x,y 计算了半径。如果计算出的半径在b/w的内径和外径之间,则触摸是在圆的边缘。希望这会对某人有所帮助。