Iphone 需要自定义UIView中的UIButton响应触摸
我有一个自定义UIView,其中有一些按钮作为子视图。我使用触摸开始/移动/结束来用用户的手指旋转视图。问题是,当我尝试从按钮拖动/旋转视图时,不会调用Iphone 需要自定义UIView中的UIButton响应触摸,iphone,uiview,touchesbegan,touchesmoved,Iphone,Uiview,Touchesbegan,Touchesmoved,我有一个自定义UIView,其中有一些按钮作为子视图。我使用触摸开始/移动/结束来用用户的手指旋转视图。问题是,当我尝试从按钮拖动/旋转视图时,不会调用touch方法。我需要按钮响应UIControlEventTouchUpInside事件,但如果它与视图一起拖动,则使我的视图旋转。我看了一个又一个问题,但两种解决方案都不适合我 以下是我的自定义视图代码: - (id)initWithFrame:(CGRect)frame andRadius:(float)Radius andViews: (
touch
方法。我需要按钮响应UIControlEventTouchUpInside
事件,但如果它与视图一起拖动,则使我的视图旋转。我看了一个又一个问题,但两种解决方案都不适合我
以下是我的自定义视图代码:
- (id)initWithFrame:(CGRect)frame andRadius:(float)Radius andViews:
(NSMutableArray *)AllViews
{
self = [super initWithFrame:frame];
if (self) {
radius = Radius;
allViews = AllViews;
for(int i = 0; i < [allViews count]; i++){
//the "currentView" is a UIButton
UIView *currentView = (UIView *)[allViews objectAtIndex:i];
[currentView setFrame:CGRectMake(frame.size.width / 2 - 25 + radius * cos((2 * M_PI / [allViews count]) * i), frame.size.height / 2 - 25 + radius * sin((2 * M_PI / [allViews count]) * i), 50, 50)];
[self addSubview:currentView];
}
[self setBackgroundColor:[UIColor redColor]];
[self setAlpha:.5];
}
return self;
}
//- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
//{
// for (UIView * view in [self subviews]) {
// if ([view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
// return YES;
// }
// }
// return NO;
//}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
x = point.x;
y = point.y;
NSLog(@"touchesBegan");
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
float X = point.x;
float Y = point.y;
float a = sqrt(X * X + Y * Y);
float b = sqrt(x * x + y * y);
float c = sqrt((x - X) * (x - X) + (y - Y) * (y - Y));
float alpha = acos((a * a + b * b - (c * c)) / (2 * a * b));
if(alpha == NAN)
alpha = 0;
alpha = -alpha;
[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.transform = CGAffineTransformRotate(self.transform, alpha);
[UIView commitAnimations];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
float X = point.x;
float Y = point.y;
float a = sqrt(X * X + Y * Y);
float b = sqrt(x * x + y * y);
float c = sqrt((x - X) * (x - X) + (y - Y) * (y - Y));
float alpha = acos((a * a + b * b - (c * c)) / (2 * a * b));
alpha = -alpha;
[UIView beginAnimations:@"rotate" context:nil];
[UIView setAnimationDuration:0.5];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
self.transform = CGAffineTransformRotate(self.transform, alpha);
[UIView commitAnimations];
NSLog(@"touchesEnded");
}
- (void)dealloc
{
[allViews release];
[super dealloc];
}
-(id)initWithFrame:(CGRect)frame和Radius:(float)Radius和view:
(NSMutableArray*)所有视图
{
self=[super initWithFrame:frame];
如果(自我){
半径=半径;
所有视图=所有视图;
对于(int i=0;i<[所有视图计数];i++){
//“currentView”是一个UI按钮
UIView*currentView=(UIView*)[allViews对象索引:i];
[currentView setFrame:CGRectMake(frame.size.width/2-25+半径*cos((2*M_-PI/[allViews count])*i)、frame.size.height/2-25+半径*sin((2*M_-PI/[allViews count])*i、50、50)];
[自添加子视图:currentView];
}
[self-setBackgroundColor:[UIColor redColor];
[自我设定α:.5];
}
回归自我;
}
//-(BOOL)pointInside:(CGPoint)pointwithevent:(UIEvent*)event
//{
//对于(UIView*在[自身子视图]中查看){
//if([view pointInside:[self-convertPoint:point-toView:view]withEvent:event]){
//返回YES;
// }
// }
//返回否;
//}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
CGPoint point=[触摸位置查看:自];
x=点x;
y=点y;
NSLog(@“触摸开始”);
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
UITouch*touch=[触摸任何对象];
CGPoint point=[触摸位置查看:自];
浮动X=点X;
浮动Y=点Y;
浮点数a=sqrt(X*X+Y*Y);
浮动b=sqrt(x*x+y*y);
浮点数c=sqrt((x-x)*(x-x)+(y-y)*(y-y));
浮动α=acos((a*a+b*b-(c*c))/(2*a*b));
如果(α==NAN)
α=0;
α=-α;
[UIView beginAnimations:@“旋转”上下文:无];
[UIView setAnimationDuration:0];
[UIView设置动画曲线:UIViewAnimationCurveEaseOut];
self.transform=CGAffineTransformRotate(self.transform,alpha);
[UIView委员会];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event
{
UITouch*touch=[触摸任何对象];
CGPoint point=[触摸位置查看:自];
浮动X=点X;
浮动Y=点Y;
浮点数a=sqrt(X*X+Y*Y);
浮动b=sqrt(x*x+y*y);
浮点数c=sqrt((x-x)*(x-x)+(y-y)*(y-y));
浮动α=acos((a*a+b*b-(c*c))/(2*a*b));
α=-α;
[UIView beginAnimations:@“旋转”上下文:无];
[UIView设置动画持续时间:0.5];
[UIView设置动画曲线:UIViewAnimationCurveEaseOut];
self.transform=CGAffineTransformRotate(self.transform,alpha);
[UIView委员会];
NSLog(@“触碰”);
}
-(无效)解除锁定
{
[所有视图发布];
[super dealoc];
}
有什么建议吗?也许不是你想要的答案,但是: 我不知道为什么您希望“手指向下按按钮,然后将手指拖动到按钮外”活动被按钮后面的视图作为拖动/旋转运动拾取。就我而言,如果你把你的手指放在一个按钮内,你是在与按钮交互,而不是与后面的视图交互;如果您在触摸屏幕的同时将手指从按钮上移开,您这样做是为了中止按钮按下(即,您已“武装”按钮,但未“触发”按钮),而不是与后面的视图交互 所以我想说的是:你为什么要这么做?也许你需要重新考虑你的UI设计 如果您确实想这样做,您可能希望使用
UIgestureRecognitizer
s,而不是touchsbegind
等-使用它们可以更轻松地设置更复杂的场景。但手势识别器并非在所有版本的iOS上都可用
或者,实现一些看起来像按钮或任何你想要的东西,并使用touchsbegind等来处理触摸操作(以及视图拖动等)——也就是说,不要使用实际的按钮。如果有人遇到类似问题,我会发布我使用的解决方案。我重写了另一个用作按钮的
UIView
类。在每个触摸开始/移动/结束
中,我实现了单击按钮所需的代码,然后将触摸命令链传递给下一个自定义ui视图
,如下所示:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesMoved:touches withEvent:event];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self];
if (point.x < self.frame.size.width && point.y < self.frame.size.height) {
NSDictionary *userInfo = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d", self.tag], @"tag", nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeFilter" object:self userInfo:userInfo];
}
[self.nextResponder touchesEnded:touches withEvent:event];
}
-(void)touchesbeated:(NSSet*)toucheevent:(UIEvent*)event
{
[self.nextResponder touchesbeated:toucheevent:event];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件
{
[self.nextResponder touchemoved:toucheevent:event];
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event
{
UITouch*touch=[触摸任何对象];
CGPoint point=[触摸位置查看:自];
if(点x
请注意,我使用NSNotifications将操作发送到包含覆盖视图的我的ViewController