-2

我有一个项目,其中有一个带有 6 个图像的 Horizo​​ntal UIScrollView,所有图像都填充了自定义图像。

当我将图像从 UIScrollView 拖放到视图顶部的 HeaderImage 上时。它更改了我将图像从 ScrollView 拖放到的区域的 Headerimage。

现在我的挑战是我的 UIScrollView 中有多个不同的图像,但是当我将 UIScrollView 中的任何图像拖放到 HeaderImage 区域时,它们都会变为相同的图像。

如何设置 UIImage 标签,以便在设置 UIScrollView 图像时保存图像,以便在拖放到 HeaderImage 区域时显示正确的图像或不同的图像。

基本上我希望能够根据 ScrollView 中的哪个图像拖放到 HeaderImage 上来更改它。

以下是我的代码:

非常感谢任何帮助。

多谢你们。我真的很感谢所有给予我的巨大帮助。

在此处输入图像描述 在此处输入图像描述

我为 GalleryScrollView.m 中的滚动视图设置了我的图像

- (void) addAttachment:(AttachmentItem *)attachment
{
// SAVE ATTACHMENT
[_attachments addObject:attachment];

// RESIZE CONTENT VIEW FOR INSERTINT NEW ATTACHMENT
_scrollView.contentSize = CGSizeMake([_attachments count]*70, 70);

CGFloat startX = (70.0f * ((float)[_attachments count] - 1.0f) + padding);
CGFloat startY = 370;
CGFloat width = 64;
CGFloat height = 64;

GalleryButton *btnAttachment = [[GalleryButton alloc] initWithFrame:CGRectMake(startX, startY, width, height)];
btnAttachment.tag = [_attachments count];
btnAttachment.scrollParent = _scrollView;
btnAttachment.mainView = self.mainView;
btnAttachment.delegate = self;

if (attachment.type == 1){
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
    imageView.image=[UIImage imageNamed:@"canadian-maple.png"];
    [btnAttachment addSubview:imageView];
    int tag = btnAttachment.tag;
    NSLog(@"%d", tag);
    [imageView release];
}else if (attachment.type == 2){
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
    imageView.image=[UIImage imageNamed:@"mozambique-wenge.png"];
    [btnAttachment addSubview:imageView];
    int tag = btnAttachment.tag;
     NSLog(@"%d", tag);
    [imageView release];
} else if (attachment.type == 3){
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
    imageView.image=[UIImage imageNamed:@"canadian-maple.png"];
    [btnAttachment addSubview:imageView];
    int tag = btnAttachment.tag;
    NSLog(@"%d", tag);
    [imageView release];
}
else if (attachment.type == 4){
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
    imageView.image=[UIImage imageNamed:@"mozambique-wenge.png"];
    [btnAttachment addSubview:imageView];
    int tag = btnAttachment.tag;
    NSLog(@"%d", tag);
    [imageView release];
}
else if (attachment.type == 5){
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
    imageView.image=[UIImage imageNamed:@"canadian-maple.png"];
    [btnAttachment addSubview:imageView];
    int tag = btnAttachment.tag;
    NSLog(@"%d", tag);
    [imageView release];
}
else if (attachment.type == 6){
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 64, 64)];
    imageView.image=[UIImage imageNamed:@"mozambique-wenge.png"];
    [btnAttachment addSubview:imageView];
    int tag = btnAttachment.tag;
    NSLog(@"%d", tag);
    [imageView release];
}

[_scrollView addSubview:btnAttachment];
[btnAttachment release];

}

并在 HegakaDragAndDropRecycleBinViewController.m 中将它们添加到我的视图中

- (void)viewDidLoad
{
[super viewDidLoad];
self.gallery.mainView = self.view;


AttachmentItem *item = [[AttachmentItem alloc] initWithData:1 data:nil];
[self.gallery addAttachment:item];
[item release];

AttachmentItem *item2 = [[AttachmentItem alloc] initWithData:2 data:nil];
[self.gallery addAttachment:item2];
[item2 release];

AttachmentItem *item3 = [[AttachmentItem alloc] initWithData:3 data:nil];
[self.gallery addAttachment:item3];
[item3 release];

AttachmentItem *item4 = [[AttachmentItem alloc] initWithData:4 data:nil];
[self.gallery addAttachment:item4];
[item4 release];

AttachmentItem *item5 = [[AttachmentItem alloc] initWithData:5 data:nil];
[self.gallery addAttachment:item5];
[item5 release];

AttachmentItem *item6 = [[AttachmentItem alloc] initWithData:6 data:nil];
[self.gallery addAttachment:item6];
[item6 release];
}

并在此处添加 TouchesEnded 的动作

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

if ([delegate isInsideRecycleBin:self touching:YES]){

    CGRect myImageRect = CGRectMake(0, 0, 320, 300);
    headerImage = [[UIImageView alloc] initWithFrame:myImageRect];
    [headerImage setImage:[UIImage imageNamed:@"light-cherry.png"]];
    [self.mainView addSubview:headerImage];

    UIImageView * animation = [[UIImageView alloc] init];
    animation.frame = CGRectMake(self.center.x - 32, self.center.y - 32, 40, 40);

    animation.animationImages = [NSArray arrayWithObjects:
                                 [UIImage imageNamed: @"iconEliminateItem1.png"],
                                 [UIImage imageNamed: @"iconEliminateItem2.png"],
                                 [UIImage imageNamed: @"iconEliminateItem3.png"],
                                 [UIImage imageNamed: @"iconEliminateItem4.png"]
                                 ,nil];
    [animation setAnimationRepeatCount:1];
    [animation setAnimationDuration:0.35];
    [animation startAnimating];
    [self.mainView addSubview:animation];
    [animation bringSubviewToFront:self.mainView];
    [animation release];
    ;
    [UIView beginAnimations:@"goback" context:nil];
    [UIView setAnimationDuration:0.4f];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.center = _originalOutsidePosition;
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
    //        loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height);
    [UIView commitAnimations];

} else{
    [UIView beginAnimations:@"goback" context:nil];
    [UIView setAnimationDuration:0.4f];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.center = _originalOutsidePosition;
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
    //        loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height);
    [UIView commitAnimations];

}

[self.delegate touchUp];

好吧,也许我在正确的轨道上。如果这是要走的路,请有人指导我,所以我补充说:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

switch ([delegate isInsideRecycleBin:self touching:YES]) {
    case 1:{

        CGRect myImageRect = CGRectMake(0, 0, 320, 300);
        headerImage = [[UIImageView alloc] initWithFrame:myImageRect];
        [headerImage setImage:[UIImage imageNamed:@"mozambique-wenge.png"]];
        headerImage.backgroundColor = [UIColor blackColor];
        headerImage.layer.borderWidth = 2;
        headerImage.layer.borderColor = [UIColor blackColor].CGColor;
        headerImage.layer.masksToBounds = YES;
        headerImage.layer.cornerRadius = 5;
        [self.mainView addSubview:headerImage];

        UIImageView * animation = [[UIImageView alloc] init];
        animation.frame = CGRectMake(self.center.x - 32, self.center.y - 32, 40, 40);

        animation.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed: @"iconEliminateItem1.png"],
                                     [UIImage imageNamed: @"iconEliminateItem2.png"],
                                     [UIImage imageNamed: @"iconEliminateItem3.png"],
                                     [UIImage imageNamed: @"iconEliminateItem4.png"]
                                     ,nil];
        [animation setAnimationRepeatCount:1];
        [animation setAnimationDuration:0.35];
        [animation startAnimating];
        [self.mainView addSubview:animation];
        [animation bringSubviewToFront:self.mainView];
        [animation release];
        ;
        [UIView beginAnimations:@"goback" context:nil];
        [UIView setAnimationDuration:0.4f];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.center = _originalOutsidePosition;
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
        //        loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height);
        [UIView commitAnimations];

        }

        break;
    case 2:{

        CGRect myImageRect = CGRectMake(0, 0, 320, 300);
        headerImage = [[UIImageView alloc] initWithFrame:myImageRect];
        [headerImage setImage:[UIImage imageNamed:@"recyclebin.png"]];
        headerImage.backgroundColor = [UIColor blackColor];
        headerImage.layer.borderWidth = 2;
        headerImage.layer.borderColor = [UIColor blackColor].CGColor;
        headerImage.layer.masksToBounds = YES;
        headerImage.layer.cornerRadius = 5;
        [self.mainView addSubview:headerImage];

        UIImageView * animation = [[UIImageView alloc] init];
        animation.frame = CGRectMake(self.center.x - 32, self.center.y - 32, 40, 40);

        animation.animationImages = [NSArray arrayWithObjects:
                                     [UIImage imageNamed: @"iconEliminateItem1.png"],
                                     [UIImage imageNamed: @"iconEliminateItem2.png"],
                                     [UIImage imageNamed: @"iconEliminateItem3.png"],
                                     [UIImage imageNamed: @"iconEliminateItem4.png"]
                                     ,nil];
        [animation setAnimationRepeatCount:1];
        [animation setAnimationDuration:0.35];
        [animation startAnimating];
        [self.mainView addSubview:animation];
        [animation bringSubviewToFront:self.mainView];
        [animation release];
        ;
        [UIView beginAnimations:@"goback" context:nil];
        [UIView setAnimationDuration:0.4f];
        [UIView setAnimationBeginsFromCurrentState:YES];
        self.center = _originalOutsidePosition;
        [UIView setAnimationDelegate:self];
        [UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
        //        loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height);
        [UIView commitAnimations];

        }
    }
    }

但没有什么大事发生

4

3 回答 3

1

GalleryButton(一个UIView子类本身)正在处理触摸。标记是 a 的属性UIView。这意味着标签已经为您准备好了:

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    if ([delegate isInsideRecycleBin:self touching:YES]){
        // Log drag to recycle bin (or as you are calling it the header image view)
        NSLog(@"View with tag %i was dragged to the recycle bin",self.tag);

        .....
于 2012-11-13T21:18:37.033 回答
0

我会回答我自己的问题。经过数周尝试不同的方法。原来我所要做的就是使用这段代码。感谢@NJones 让我走上了正轨。

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if ([delegate isInsideRecycleBin:self touching:YES]){

    if (self.tag == 1) {

        NSLog(@"View with tag %i was dropped onto trigger area",self.tag);

        CGRect myImageRect = CGRectMake(0, 0, 320, 300);
        headerImage = [[UIImageView alloc] initWithFrame:myImageRect];
        [headerImage setImage:[UIImage imageNamed:@"light-cherry.png"]];
        headerImage.backgroundColor = [UIColor blackColor];
        headerImage.layer.borderWidth = 2;
        headerImage.layer.borderColor = [UIColor blackColor].CGColor;
        headerImage.layer.masksToBounds = YES;
        headerImage.layer.cornerRadius = 5;
        [self.mainView addSubview:headerImage];

    }
    else if (self.tag == 2)
    {

        NSLog(@"View with tag %i was dropped onto trigger area",self.tag);

        CGRect myImageRect = CGRectMake(0, 0, 320, 300);
        headerImage = [[UIImageView alloc] initWithFrame:myImageRect];
        [headerImage setImage:[UIImage imageNamed:@"mozambique-wenge.png"]];
        headerImage.backgroundColor = [UIColor blackColor];
        headerImage.layer.borderWidth = 2;
        headerImage.layer.borderColor = [UIColor blackColor].CGColor;
        headerImage.layer.masksToBounds = YES;
        headerImage.layer.cornerRadius = 5;
        [self.mainView addSubview:headerImage];
    }
    UIImageView * animation = [[UIImageView alloc] init];
    animation.frame = CGRectMake(self.center.x - 32, self.center.y - 32, 40, 40);

    animation.animationImages = [NSArray arrayWithObjects:
                                 [UIImage imageNamed: @"iconEliminateItem1.png"],
                                 [UIImage imageNamed: @"iconEliminateItem2.png"],
                                 [UIImage imageNamed: @"iconEliminateItem3.png"],
                                 [UIImage imageNamed: @"iconEliminateItem4.png"]
                                 ,nil];
    [animation setAnimationRepeatCount:1];
    [animation setAnimationDuration:0.35];
    [animation startAnimating];
    [self.mainView addSubview:animation];
    [animation bringSubviewToFront:self.mainView];
    [animation release];
    ;
    [UIView beginAnimations:@"goback" context:nil];
    [UIView setAnimationDuration:0.4f];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.center = _originalOutsidePosition;
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
    //        loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height);
    [UIView commitAnimations];

} else{
    [UIView beginAnimations:@"goback" context:nil];
    [UIView setAnimationDuration:0.4f];
    [UIView setAnimationBeginsFromCurrentState:YES];
    self.center = _originalOutsidePosition;
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector: @selector(animationDidStop:finished:context:)];
    //        loadingView.frame = CGRectMake(rect.origin.x, rect.origin.y - 80, rect.size.width, rect.size.height);
    [UIView commitAnimations];

}

[self.delegate touchUp];

}
于 2012-11-14T09:13:12.473 回答
0

在 touchesBegan 上,保存您开始的“附件项目”!?

于 2012-11-13T20:26:25.950 回答