在我目前的ios项目中,我将屏幕的一侧专用于一个对象,屏幕的另一侧专用于另一个对象,我已将其设置为如果您在屏幕的一侧滑过手指指定的对象会移动。但是,我想做到这一点,你可以在不同的运动中同时移动两个对象,但我无法弄清楚如何这样做。以下是我目前的代码。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (location.x <= 270 ) {
[Person setCenter:CGPointMake(location.x, Person.center.y)];
}
else {
[Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
if (location.x <= 270 ) {
[Person setCenter:CGPointMake(location.x, Person.center.y)];
}
else {
[Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
}
}
答案 0 :(得分:2)
你应该开始处理touches
集合中提供的多个触摸 - 循环遍历所有UITouch
个对象并进行处理。
编辑: 这是你的代码:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *touch in [event allTouches]) {
CGPoint location = [touch locationInView:touch.view];
if (location.x <= 270 ) {
[Person setCenter:CGPointMake(location.x, Person.center.y)];
}
else {
[Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
for(UITouch *touch in [event allTouches]) {
CGPoint location = [touch locationInView:touch.view];
if (location.x <= 270 ) {
[Person setCenter:CGPointMake(location.x, Person.center.y)];
}
else {
[Person1 setCenter:CGPointMake(location.x, Person1.center.y)];
}
}
}
答案 1 :(得分:0)
如果你移动-touchesBegan&amp; touchesMoved代码到Person视图类而不是它当前所在的view /或viewController类,然后这些视图可以处理彼此独立且同时处理的触摸。
*编辑:更多信息: 目前,您正在使用上面粘贴的代码处理触摸事件(在我猜测UIViewController),如果您将该代码移动到您创建的Person类中,您可以使代码更简单并获得您想要的结果。 这将导致人员决定是否被触摸以及相应地移动自己。
在Person.m文件中添加此代码
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.superview];
[self moveToLocation:location];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.superview];
[self moveToLocation:location];
}
-(void)moveToLocation:(CGPoint)location{
CGFloat halfOfPersonWidth = self.bounds.size.width /2.0f;
CGFloat halfOfSuperviewWidth = self.superview.bounds.size.width/2.0f;
// Stop Person from going off left screen edge
if ((location.x - halfOfPersonWidth) <= 0.0f){
return;
} // Stop Person from going off right screen edge
else if ((location.x + halfOfPersonWidth) >= self.superview.bounds.size.width){
return;
}
if (self.center.x < halfOfSuperviewWidth) {
// Person is on the left of the screen and should not move to right side
if ((location.x + halfOfPersonWidth) > halfOfSuperviewWidth) {
return;
}
} else{
// Person is on the right of the screen and should not move to left side
if ((location.x - halfOfPersonWidth) < halfOfSuperviewWidth) {
return;
}
}
// If we have made it this far then we can move the Person
// move to touch location on x axis only
[self setCenter:CGPointMake(location.x, self.center.y)];
}
现在,在View Controller类中,您可以删除原始-touchesBegan&amp; -touchesMoved你在这里粘贴的代码,或者如果你想谨慎就把它评论出来
/* put slash asterisks above the code and asterisks slash below the code to comment out */
如果您构建并运行,您应该能够像以前一样移动每个Person视图,但如果您同时将手指放在每个Person视图上,您应该能够同时移动它们。