UILongPressGestureRecognizer *longPress = [[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(photoLongPressed:)] autorelease];
longPress.numberOfTouchesRequired = 1;
[puzzlesScroll addGestureRecognizer:longPress];
[puzzlesScroll release];
- (void)photoLongPressed:(UILongPressGestureRecognizer *)gesture
{
PuzzlesScrollVIew *layoutPhotoView = (id)[gesture view];
if(gesture.state == UIGestureRecognizerStateBegan) //当前状态的手势识别
{
startTouchPos = [gesture locationInView:puzzleBackground];
CGRect photoFrame = layoutPhotoView.frame;
startTouchPos.x -= photoFrame.origin.x;
startTouchPos.y -= photoFrame.origin.y;
srcRect = photoFrame;
destRect = photoFrame;
[puzzleBackground bringSubviewToFront:layoutPhotoView];
layoutPhotoView.layer.borderWidth = 2.0;
layoutPhotoView.layer.borderColor = [UIColor yellowColor].CGColor;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
[UIView commitAnimations];
self.view.userInteractionEnabled = NO;
}
else if(gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled)//识别已收到确认涉及的姿态结束 与 识别已收到涉及取消的姿态
{
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
layoutPhotoView.frame = destRect;
[layoutPhotoView restoreAll];
layoutPhotoView.layer.borderColor = [UIColor whiteColor].CGColor;
layoutPhotoView.layer.borderWidth = 1.0;
[UIView commitAnimations];
self.view.userInteractionEnabled = YES;
}
else if(gesture.state == UIGestureRecognizerStateChanged) //识别作为一个手势的变化
{
CGRect photoFrame = layoutPhotoView.frame;
CGPoint touchPoint = [gesture locationInView:puzzleBackground];
photoFrame.origin.x = touchPoint.x - startTouchPos.x;
photoFrame.origin.y = touchPoint.y - startTouchPos.y;
layoutPhotoView.frame = photoFrame;
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3];
for(PuzzlesScrollVIew * otherLayoutPhotoView in scrollimgViewArr)
{
if(otherLayoutPhotoView != layoutPhotoView && CGRectContainsPoint(otherLayoutPhotoView.frame, touchPoint))
{
destRect = otherLayoutPhotoView.frame;
otherLayoutPhotoView.frame = srcRect;
//交换对象
[scrollimgViewArr exchangeObjectAtIndex:[scrollimgViewArr indexOfObject:layoutPhotoView] withObjectAtIndex:[scrollimgViewArr indexOfObject:otherLayoutPhotoView]];
[otherLayoutPhotoView restoreAll];
srcRect = destRect;
otherLayoutPhotoView.layer.borderColor = [UIColor whiteColor].CGColor;
otherLayoutPhotoView.layer.borderWidth = 1.0;
break;
}
}
[UIView commitAnimations];
}
}