Как мне получить доступ только к одному из моих касаний в событии "Мои касания начались"
-
21-09-2019 - |
Вопрос
У меня есть:
UITouch *touch = [touches anyObject];
if ([touches count] == 2) {
//preforming actions
}
Что я хочу сделать, так это задать это внутри if
утверждение, в котором два касания выполняются отдельно.
Решение
Вы можете перебирать штрихи:
if([touches count] == 2) {
for(UITouch *aTouch in touches) {
// Do something with each individual touch (e.g. find its location)
}
}
Редактировать:если вы хотите, скажем, найти расстояние между двумя касаниями, и вы знаете, что их ровно два, вы можете взять каждое отдельно, а затем произвести некоторые вычисления.Пример:
float distance;
if([touches count] == 2) {
// Order touches so they're accessible separately
NSMutableArray *touchesArray = [[[NSMutableArray alloc]
initWithCapacity:2] autorelease];
for(UITouch *aTouch in touches) {
[touchesArray addObject:aTouch];
}
UITouch *firstTouch = [touchesArray objectAtIndex:0];
UITouch *secondTouch = [touchesArray objectAtIndex:1];
// Do math
CGPoint firstPoint = [firstTouch locationInView:[firstTouch view]];
CGPoint secondPoint = [secondTouch locationInView:[secondTouch view]];
distance = sqrtf((firstPoint.x - secondPoint.x) *
(firstPoint.x - secondPoint.x) +
(firstPoint.y - secondPoint.y) *
(firstPoint.y - secondPoint.y));
}
Другие советы
Касания - это уже массив.Нет необходимости копировать их в другой массив - просто используйте [touches objectAtIndex:n] для доступа к touch n.
Не связан с StackOverflow