サブビューとしていくつかのボタンを持つカスタム 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];
}
助言がありますか?