1

添加UIImageViewUIScrollView添加UIPanGestureRecognizer到它

UIImageView* lbl = [[UIImageView alloc]init];
NSString* tempStr = [NSString stringWithFormat:@"images/%@",[self.cloudArray objectAtIndex:i]];
NSString* imgPath = [self appendDocumentDirectoryPath:tempStr];          
lbl.image = [[[UIImage alloc] initWithContentsOfFile:imgPath] autorelease];
[lbl setUserInteractionEnabled:YES];
//add gestures
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(moveImage:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:1];
[lbl addGestureRecognizer:panGesture];
[panGesture release];
CGSize expectedLabelSize = CGSizeMake(150, height);
lbl.frame = CGRectMake(pntX, pntY, expectedLabelSize.width, height);
[rectFrame addObject:[NSValue valueWithCGRect:lbl.frame]];
lbl.tag = i;
[cloudScrollView addSubview:lbl];
[lbl release];
lbl = nil;
pntX = pntX + space + expectedLabelSize.width;
[cloudScrollView setContentSize:CGSizeMake([cloudArray count]*170, 160)];

#pragma mark METHOD TO CATCH MOVE GESTURE EVENTS

- (void)moveImage:(UIPanGestureRecognizer *)gesture 
{
    if (gesture.state == UIGestureRecognizerStateChanged) {
        location1 = [gesture locationInView:cloudScrollView];
        view1 = [gesture view];
        if (isFirstTime) {
            isFirstTime = NO;
            draggedTag = view1.tag;
        }
        [view1 setCenter:CGPointMake(location1.x, location1.y)];

    }
    if (gesture.state == UIGestureRecognizerStateEnded) {

        CGPoint pnt = [gesture locationInView:customTableView];

        NSIndexPath* indexPath = [customTableView indexPathForRowAtPoint:pnt];
        NSLog(@"indexpath = %d",indexPath.row);
        if (index >= 0) {
            [self.ansArray replaceObjectAtIndex:indexPath.row withObject:[self.cloudArray objectAtIndex:draggedTag]];//[self.cloudArray objectAtIndex:draggedTag]
            [customTableView reloadData];
        }
        CGRect draggedItemRect = [[rectFrame objectAtIndex:draggedTag] CGRectValue];
        view1.frame = draggedItemRect;
        isFirstTime = YES;
    }

}
4

0 回答 0