当前位置: 动力学知识库 > 问答 > 编程问答 >

ios - Picture tapped from UIPickerView

问题描述:

Good afternoon,

I have a UIPickerView that shows pictures of birds, different picture with different row selected in UIPickerView, now I want to tap the picture for more information about the bird, so I am thinking about a TextView that pops up when the picture is tapped. How would I implement that in my code? I am new to programming and any help is much appreciated. This is my code so far.

- (void)viewDidLoad

{

[super viewDidLoad];

// Do any additional setup after loading the view.

//Load up NSArray object

_fuglar = [[NSArray alloc]initWithObjects:@"Allt árið",@"Hrafn", @"Svartbakur",@"Silfurmáfur",@"Sílamáfur",@"Frá 20.ágúst til 15.mars",@"Grágæs",@"Heiðagæs",@"Frá 1.september til 15.mars",@"Fýll",@"Dílaskarfur",@"Toppskarfur",@"Helsingi",@"Stokkönd", @"Urtönd",@"Rauðhöfðaönd",@"Duggönd",@"Skúfönd",@"Hávella",@"Toppönd",@"Hvítmáfur",@"Hettumáfur",@"Rita",@"Frá 15.apríl til 14.júlí",@"Kjói(í og við friðlýst æðarvarp)",@"Frá 1.september til 25.apríl",@"Álka",@"Langvía",@"Stuttnefja",@"Teista",@"Lundi",@"-Breytilegt-",@"Rjúpa", nil];

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

#pragma mark - UIPickerView Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

return 1;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

return _fuglar.count;

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{

return [_fuglar objectAtIndex:row];

}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

{

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 300, 37)];

label.textAlignment = NSTextAlignmentCenter;

label.backgroundColor = [UIColor clearColor];

label.font = [UIFont fontWithName:@"Helvetica" size:22];

if (row == 0 || row == 5 || row == 8 || row == 23 || row == 25 || row == 31) {

label.textColor = [UIColor redColor];

}else{

label.textColor = [UIColor blackColor];

}

label.text = [_fuglar objectAtIndex:row];

return label;

if (row == 0) {

[UIImage imageNamed:@"byssa2"];

} else {

[_fuglar objectAtIndex:row];

}

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

//Create UIImage objects

UIImage * byssa2Image = [UIImage imageNamed:@"byssa2.jpg"];

UIImage * hrafnImage = [UIImage imageNamed:@"hrafn.jpg"];

UIImage * svartbakurImage = [UIImage imageNamed:@"Svartbakur.jpg"];

UIImage * silfurmafurImage = [UIImage imageNamed:@"Silfurmafur.jpg"];

UIImage * silamafurImage = [UIImage imageNamed:@"Silamafur.jpg"];

UIImage * gragaesImage = [UIImage imageNamed:@"Gragaes.jpg"];

UIImage * heidargaesImage = [UIImage imageNamed:@"Heidagaes.jpg"];

UIImage * fyllImage = [UIImage imageNamed:@"fyll.jpg"];

UIImage * toppskarfurImage = [UIImage imageNamed:@"Dilaskarfur1.jpg"];

UIImage * dilaskarfurImage = [UIImage imageNamed:@"Toppskarfur.jpg"];

UIImage * helsingiImage = [UIImage imageNamed:@"Helsingi.jpg"];

UIImage * stokkondImage = [UIImage imageNamed:@"Stokkond.jpg"];

UIImage * urtondImage = [UIImage imageNamed:@"Urtond.jpg"];

UIImage * raudhofdaondImage = [UIImage imageNamed:@"Raudhofdaond.jpg"];

UIImage * duggondImage = [UIImage imageNamed:@"Duggond.jpg"];

UIImage * skufondImage = [UIImage imageNamed:@"Skufond.jpg"];

UIImage * havellaImage = [UIImage imageNamed:@"Havella.jpg"];

UIImage * toppondImage = [UIImage imageNamed:@"Toppond.jpg"];

UIImage * hvitmafurImage = [UIImage imageNamed:@"Hvitmafur.jpg"];

UIImage * hettumafurImage = [UIImage imageNamed:@"Hettumafur.jpg"];

UIImage * ritaImage = [UIImage imageNamed:@"Rita.jpg"];

UIImage * alkaImage = [UIImage imageNamed:@"Alka.jpg"];

UIImage * langviaImage = [UIImage imageNamed:@"Langvia1.jpg"];

UIImage * stuttnefjaImage = [UIImage imageNamed:@"Stuttnefja.jpg"];

UIImage * teistaImage = [UIImage imageNamed:@"Teista1.jpg"];

UIImage * lundiImage = [UIImage imageNamed:@"Lundi.jpg"];

UIImage * rjupaImage = [UIImage imageNamed:@"Rjupa1.jpg"];

UIImage * kjoiImage = [UIImage imageNamed:@"Kjoi.jpg"];

switch (row) {

break;

case 0:

fuglarImage.image = byssa2Image;

break;

case 1:

fuglarImage.image = hrafnImage;

break;

case 2:

fuglarImage.image = svartbakurImage;

break;

case 3:

fuglarImage.image = silfurmafurImage;

break;

case 4:

fuglarImage.image = silamafurImage;

break;

case 6:

fuglarImage.image = gragaesImage;

break;

case 7:

fuglarImage.image = heidargaesImage;

break;

case 9:

fuglarImage.image = fyllImage;

break;

case 10:

fuglarImage.image = toppskarfurImage;

break;

case 11:

fuglarImage.image = dilaskarfurImage;

break;

case 12:

fuglarImage.image = helsingiImage;

break;

case 13:

fuglarImage.image = stokkondImage;

break;

case 14:

fuglarImage.image = urtondImage;

break;

case 15:

fuglarImage.image = raudhofdaondImage;

break;

case 16:

fuglarImage.image = duggondImage;

break;

case 17:

fuglarImage.image = skufondImage;

break;

case 18:

fuglarImage.image = havellaImage;

break;

case 19:

fuglarImage.image = toppondImage;

break;

case 20:

fuglarImage.image = hvitmafurImage;

break;

case 21:

fuglarImage.image = hettumafurImage;

break;

case 22:

fuglarImage.image = ritaImage;

break;

case 24:

fuglarImage.image = kjoiImage;

break;

case 26:

fuglarImage.image = alkaImage;

break;

case 27:

fuglarImage.image = langviaImage;

break;

case 28:

fuglarImage.image = stuttnefjaImage;

break;

case 29:

fuglarImage.image = teistaImage;

break;

case 30:

fuglarImage.image = lundiImage;

break;

case 32:

fuglarImage.image = rjupaImage;

break;

default:

break;

}

}

网友答案:

First I think what you need is a tap gesture on the picker itself, then when it's tapped you can decide what to do.. pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component doesn't quite work the way you want it too right?

First off, wherever you create your picker / create your picker property

Something like this

-(UIPickerView *)pickerView
{
    if (!_pickerView) {

        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectZero];
        _pickerView.dataSource = self;
        _pickerView.delegate = self;

        UITapGestureRecognizer *tapGestureRecogniser = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickerTapped:)];
        tapGestureRecogniser.cancelsTouchesInView = NO;
        tapGestureRecogniser.delegate = self;
        [_pickerView addGestureRecognizer:tapGestureRecogniser];
    }

    return _pickerView;
}

After this you need to add a little bit to make sure our tap gesture doesn't interfere with our pickers motion we need to add UIGestureRecognizerDelegate like so

@interface MYPickerViewController () <UIPickerViewDataSource, UIPickerViewDelegate, UIGestureRecognizerDelegate>

and also

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {

    return YES;
}

Now that's out of the way... we can handle tapping the picker. I've used CELL_HEIGHT to represent the height of your picker cells

-(void)pickerTapped:(UITapGestureRecognizer *)tapGestureRecogniser
{
    NSLog(@"pickerTapped");

    CGPoint touchPoint = [tapGestureRecogniser locationInView:[self picker]];

        if (touchPoint.y < ([self picker].frame.size.height + CELL_HEIGHT)/2 && touchPoint.y > ([self picker].frame.size.height/2) - (CELL_HEIGHT/2)) // this makes sure the tap was actually on the selected cell
    {
        NSLog(@"Met tap criteria");

        NSInteger row = [self.pickerView selectedRowInComponent:0];
        [self presentInfoForRow:row];
    }
}

Finally the bit you asked for you want an alert to pop up

-(void)presentInfoForRow:(NSInteger)row
{

     NSString *title = @"";
     NSString *message = @"";

     // change your message depending on the row selected.
     // you used a switch above, but you'd be much better off creating a bird object, putting .image .title and .message fields in and sticking them in an array

    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];

        UIAlertAction *doneAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Done", @"") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {

        }];

        [alertController addAction:doneAction];

    [self.navigationController presentViewController:alertController animated:YES completion:nil];

}
分享给朋友:
您可能感兴趣的文章:
随机阅读: