2012-11-12 71 views
1

我有一個應用程序,我將圖像從一個滾動視圖拖放到一個區域,當滾動視圖中的圖像被拖放到圖像所在的區域時,該區域的圖像被更改。觸摸結束多個圖像

我的問題是如何將標題中的圖像更改爲拖到該區域的圖像。

目前我在滾動視圖中有不同的圖像,並且拖動和放置到我定義的相同圖像區域時出現所有這些圖像。

我該如何爲我的ScrolView中的每個圖像製作不同的觸控事件。

enter image description here

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

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

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


    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]; 


} 

以上是對圖像的代碼被丟棄的事件。

下面是我如何設置我的按鈕在我ScrolView最多

- (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]; 
} 

- (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]; 
} 

所以現在我試圖讓一個數組我被拖到改變該區域的圖像HeaderImage,但我又被卡住了。我正在爲我的項目所需的最後一項功能,我所要做的就是根據拾取的圖像更改視圖頂部的HeaderImage,並將其拖放到標題圖像區域。

代碼編輯

會像這樣的工作?

// Create the UIImage's from the images folder 
UIImage *app = [UIImage imageNamed:@"Appstorm.jpg"]; 
UIImage *creat = [UIImage imageNamed:@"Creattica.jpg"]; 
UIImage *free = [UIImage imageNamed:@"Freelance.jpg"]; 
UIImage *net = [UIImage imageNamed:@"Netsetter.jpg"]; 
UIImage *rock = [UIImage imageNamed:@"Rockable.jpg"]; 
UIImage *tuts = [UIImage imageNamed:@"Tutsplus.jpg"]; 
UIImage *work = [UIImage imageNamed:@"Workawesome.jpg"]; 

/* Create the dummy array and fill with the UIImages. */ 
NSArray *banners = [[NSArray alloc] initWithObjects:app, creat, free, net, rock, tuts, work, nil]; 

// Set the main images array equal to the dummy array 
self.bannerImages = banners; 

// Release the dummy array 
[banners release]; 

// Release the UIImage objects 
[app release]; 
[creat release]; 
[free release]; 
[net release]; 
[rock release]; 
[tuts release]; 
[work release]; 

detailVC.banner = [bannerImages objectAtIndex:indexPath.row]; 

imageView.image = banner; 

} 

我也把圖片標籤放在我的圖片中。我將如何使用這些來做我想做的事。

+1

爲什麼不使用標籤爲您的圖像視圖,並顯示圖像被拖曳的圖像的標籤...? –

+0

@Sharanya我現在已經爲我的圖片製作了標籤。 int tag = btnAttachment.tag; NSLog(@「%d」,tag);如何使用這些標籤將縮略圖滾動視圖圖像載入圖像視圖區域> –

回答

0

好的。所以我用了一個if else語句來調用我的標籤。 :)

-(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]; 
} 
0

這是很多的代碼來閱讀。 我可以建議你在屏幕截圖上使用自定義滾動視圖來顯示小圖像。它會爲你節省很多時間