Я реализовал квадратную ничью, используя UIBezierPath. Чтобы переместить один и тот же квадрат, я применил UILongPressGestureRecognizer, чтобы проверить, нажата ли квадрат, и свободно перемещать квадрат в подвью.UIGestureRecognizerStateChangedv вызывается дважды при перемещении изображения безье в subView
Вот некоторые из кода:
square.h
...
//square coordinates and size
static const float xPoint = 10;
static const float yPoint = 16;
static const float Width = 10;
static const float Height = 20;
//bezier BitMap context
static const float contextWidht = 300;
static const float contextHeigh = 300;
...
square.m
...
-(void)build{
UIBezierPath *squareDraw = [UIBezierPath bezierPathWithRect:CGRectMake(xPoint, yPoint, Width, Height)];
UIGraphicsBeginImageContext(CGSizeMake(contextWidht, contextHeigh));
// Graphic Context
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
[squareDraw fill];
[squareDraw stroke];
// Get image from Graphic Context
UIImage *bezierImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *bezierImageView = [[UIImageView alloc]initWithImage:bezierImage];
//subView
_frame = bezierImageView;
[_frame setUserInteractionEnabled:YES];
//Gesture Recognizer
UILongPressGestureRecognizer *tapGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(tapHandler:)];
tapGesture.delegate = (id)self;
[_frame addGestureRecognizer:tapGesture];
[self addSubview:_frame];
}
- (void)tapHandler:(UILongPressGestureRecognizer *) sender{
sender.delegate = (id)_frame;
CGPoint touchLocation = [sender locationInView:_frame];
CGFloat xVariation, yVariation;
...
case UIGestureRecognizerStateChanged:
xVariation = [sender locationInView:_frame].x;
yVariation = [sender locationInView:_frame].y;
_frame.center = CGPointMake(xVariation, yVariation);
break;
....
}
Похоже, при отладке приложения (нажмите и переместите _frame), «UIGestureRecognizerStateChanged» вызывается дважды и возвращение чередовались точек между подвидом и некоторыми моментами, которые результат из контекстаWidth и contextHeight:
2013-07-22 14:55:00.327 bezier[10847:11603] xVariation: 22.000000
2013-07-22 14:55:00.329 bezier[10847:11603] yVariation: 28.000000
2013-07-22 14:55:00.330 bezier[10847:11603] xVariation: 150.000000 ----> ??
2013-07-22 14:55:00.330 bezier[10847:11603] yVariation: 150.000000 ----> ??
2013-07-22 14:55:00.332 bezier[10847:11603] xVariation: 29.000000
2013-07-22 14:55:00.332 bezier[10847:11603] yVariation: 28.000000
Почему это так?
Дело в том, что мне нужны непрерывные сигналы и вам нужно знать все время, когда квадратное положение связано с подвид. Я не знаю, почему аргументы UIGraphicsBeginImageContext влияют на квадратную позицию. –
Я не думаю, что UIGraphicsBeginImageContext влияет на позиции распознавателя жестов, однако вы можете узнать, почему это происходит, когда вы выходите из режима просмотра каждого события. Может быть, странные местоположения происходят совершенно по-другому, и вам может понадобиться включить их также в свой код? – Fuggly