2011年8月16日 星期二

令UIImageView擁有簡單的Move, Zoom, Delete, Rotate功能

- (void)viewDidLoad
{
UIImageView* iconView = [[UIImageView alloc] initWithImage:icon];
[iconView setCenter:CGPointMake(self.view.frame.size.width / 2, self.view.frame.size.height / 2)];
UILongPressGestureRecognizer* longPressGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[iconView addGestureRecognizer:longPressGesture];
[longPressGesture release];
UIRotationGestureRecognizer* rotationGesture = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[iconView addGestureRecognizer:rotationGesture];
[rotationGesture release];
UIPinchGestureRecognizer* pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[iconView addGestureRecognizer:pinchGesture];
[pinchGesture release];
UIPanGestureRecognizer* panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleGesture:)];
[iconView addGestureRecognizer:panGesture];
[panGesture release];
iconView.userInteractionEnabled = TRUE;
[self.view addSubview:iconView];
}

#pragma mark -
#pragma mark Gesture Callback
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];

piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}

-(void)handleGesture:(UIGestureRecognizer*)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];

// Rotate
if([gestureRecognizer isMemberOfClass:[UIRotationGestureRecognizer class]])
{
UIRotationGestureRecognizer* rotationGestureRecognizer = (UIRotationGestureRecognizer*)gestureRecognizer;
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged)
{
[rotationGestureRecognizer view].transform = CGAffineTransformRotate([[rotationGestureRecognizer view] transform], [rotationGestureRecognizer rotation]);
[rotationGestureRecognizer setRotation:0];
}
}
// Zoom
else if([gestureRecognizer isMemberOfClass:[UIPinchGestureRecognizer class]])
{
UIPinchGestureRecognizer* pinchGestureRecognizer = (UIPinchGestureRecognizer*)gestureRecognizer;
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged)
{
[pinchGestureRecognizer view].transform = CGAffineTransformScale([[pinchGestureRecognizer view] transform], [pinchGestureRecognizer scale], [pinchGestureRecognizer scale]);
[pinchGestureRecognizer setScale:1];
}
}
// Menu - Delete
else if([gestureRecognizer isMemberOfClass:[UILongPressGestureRecognizer class]])
{
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan)
{
UIMenuController *menuController = [UIMenuController sharedMenuController];
UIMenuItem *resetMenuItem = [[UIMenuItem alloc] initWithTitle:@"Reset" action:@selector(deleteMenu:)];
CGPoint location = [gestureRecognizer locationInView:[gestureRecognizer view]];


[self becomeFirstResponder];
[menuController setMenuItems:[NSArray arrayWithObject:resetMenuItem]];
[menuController setTargetRect:CGRectMake(location.x, location.y, 0, 0) inView:[gestureRecognizer view]];
[menuController setMenuVisible:YES animated:YES];


[resetMenuItem release];
}
}
// Move
else if([gestureRecognizer isMemberOfClass:[UIPanGestureRecognizer class]])
{
UIView* piece = [gestureRecognizer view];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged)
{
UIPanGestureRecognizer* panGestureRecognizer = (UIPanGestureRecognizer*)gestureRecognizer;
CGPoint translation = [panGestureRecognizer translationInView:[piece superview]];


[piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
[panGestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
}
}
}

沒有留言:

張貼留言