Domanda

buttonTapped method check if it is an small iphone, normal iphone or ipad. Set the image name and size accordingly. There is no problem with it, works as expected.

When clicked on the "pressedClassicButton" it reloads some data in other views and changes images of some other uiviews. it should not touch imageView in any way. The data it changes are not connected to imageView also.

But somehow when i call the "pressedClassicButton" method, it starts ignoring my specified ipad frame sizes and sets the imageView size as specified on the storyboard constraints. This problem only happens in ipad.

The constraints are: 1:1 ratio, 20px to left right and top.

- (IBAction)pressedClassicButton:(id)sender {
    [Flurry logEvent:@"Pressed->SelectPage->Classic"];
    [self toggleButton:sender];
    self.currentArray = self.classicArray;
    [self.swipeView reloadData];
}

-(void)toggleButton:(UIButton *)button{
    UIImage *inactiveButton = [UIImage imageNamed:@"fruitify_tab_inactive.png"];
    UIImage *activeButton = [UIImage imageNamed:@"fruitify_tab_active.png"];

    [self.buttonClassicOut setBackgroundImage:inactiveButton forState:UIControlStateNormal];
    [self.buttonGroupOut setBackgroundImage:inactiveButton forState:UIControlStateNormal];
    [self.buttonSpecialOut setBackgroundImage:inactiveButton forState:UIControlStateNormal];
    [self.buttonTropicalOut setBackgroundImage:inactiveButton forState:UIControlStateNormal];

    [button setBackgroundImage:activeButton forState:UIControlStateNormal];
}


- (void)buttonTapped:(UIButton *)sender
{
    int clicked = (int)sender.tag;

    self.lastChoice = clicked;
    NSString *str = self.currentArray[clicked][3];

    for (UIView *subView in self.imageView.subviews)
    {
        if (subView.tag < 120 && subView.tag > 100)
        {
            [subView removeFromSuperview];
        }
    }

    NSString *machineName = [[NSString alloc] initWithString:[MasterClass MachineName]];

    if ([machineName rangeOfString:@"iPad"].location != NSNotFound) {
        NSLog(@"ipad");
        self.imageView.frame = CGRectMake(120.0, 80.0, 520, 520);
    } else if ([machineName rangeOfString:@"iPhone3"].location != NSNotFound)  {
        self.imageView.frame = CGRectMake(50.0, 70.0, 220, 220);
        NSLog(@"iphone3-4");
    } else if ([machineName rangeOfString:@"iPod"].location != NSNotFound)  {
        self.imageView.frame = CGRectMake(50.0, 70.0, 220, 220);;
        NSLog(@"iphone3-4");
    }
    else{
        self.imageView.frame = CGRectMake(20.0, 82.0, 280, 280);
        NSLog(@"iphone5+");
    }

    self.imageView.image = [UIImage imageNamed:self.currentArray[clicked][4]];
    [self.imageView setUserInteractionEnabled:YES];

    float realImageSize = self.imageView.image.size.height;
    float viewBoundSize = self.imageView.bounds.size.height;

    float sizeDifference = realImageSize / viewBoundSize;

    if (![str  isEqual: @"group"]) {
        CGPoint eye = CGPointMake([self.currentArray[clicked][8] floatValue] / sizeDifference, [self.currentArray[clicked][9] floatValue] / sizeDifference);
        CGPoint mouth = CGPointMake([self.currentArray[clicked][10] floatValue] / sizeDifference, [self.currentArray[clicked][11] floatValue] / sizeDifference);

        CGPoint center = CGPointMake( (eye.x + mouth.x)/2 , (eye.y + mouth.y)/2 );

        CGFloat xDist = (eye.x - mouth.x);
        CGFloat yDist = (eye.y - mouth.y);
        CGFloat faceSize = sqrt((xDist * xDist) + (yDist * yDist));

        UIImage *tapFace = [UIImage imageNamed:@"fruitify_face_button.png"];

        CGRect newBound = CGRectMake(center.x - faceSize, center.y - faceSize, faceSize*2, faceSize*2);

        UIButton *tapButton = (UIButton *)self.imageView;
        tapButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [tapButton setFrame:newBound];
        [tapButton setBackgroundImage:tapFace forState:UIControlStateNormal];
        tapButton.userInteractionEnabled = YES;
        tapButton.tag = 113;
        tapButton.enabled = YES;
        [tapButton addTarget:self action:@selector(tapHereTapped:) forControlEvents:UIControlEventTouchUpInside];

        [self.imageView addSubview:tapButton];

    }else if([str isEqual:@"group"]){
        int faceCount = [self.currentArray[clicked][8] count];

        for (int i = 0; i < faceCount; i++) {

            CGPoint eye = CGPointMake([self.currentArray[clicked][8][i] floatValue] / sizeDifference, [self.currentArray[clicked][9][i] floatValue] / sizeDifference);
            CGPoint mouth = CGPointMake([self.currentArray[clicked][10][i] floatValue] / sizeDifference, [self.currentArray[clicked][11][i] floatValue] / sizeDifference);

            CGPoint center = CGPointMake( (eye.x + mouth.x)/2 , (eye.y + mouth.y)/2 );

            CGFloat xDist = (eye.x - mouth.x);
            CGFloat yDist = (eye.y - mouth.y);
            CGFloat faceSize = sqrt((xDist * xDist) + (yDist * yDist));

            UIImage *tapFace = [UIImage imageNamed:@"fruitify_face_button.png"];

            CGRect newBound = CGRectMake(center.x - faceSize, center.y - faceSize, faceSize*2, faceSize*2);

            UIButton *tapButton = (UIButton *)self.imageView;
            tapButton = [UIButton buttonWithType:UIButtonTypeCustom];
            [tapButton setFrame:newBound];
            [tapButton setBackgroundImage:tapFace forState:UIControlStateNormal];
            tapButton.userInteractionEnabled = YES;
            tapButton.tag = 113 + i;
            tapButton.enabled = YES;
            [tapButton addTarget:self action:@selector(tapHereTapped:) forControlEvents:UIControlEventTouchUpInside];

            [self.imageView addSubview:tapButton];
        }
    }
}
È stato utile?

Soluzione

Unexpected things happen when you use constraints and try to set frames as well. Try setting the sizes of your views by altering their constraints instead. You add them as properties from your storyboard by ctrl click drag like you do when hooking up a button.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top