我有一个带有一些按钮作为子视图的自定义 UIView。我正在使用 touchesBegan/Moved/Ended 来用用户的手指旋转视图。问题是,当我尝试从按钮拖动/旋转视图时,touches
不会调用这些方法。我需要按钮来响应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];
}
有什么建议么?