iPhone和ipad模拟器图像大小不符合它应有的行为

时间:2014-05-05 12:57:29

标签: iphone ipad uiimageview

buttonTapped方法检查它是小型iphone,普通iphone还是ipad。相应地设置图像名称和大小。它没有问题,按预期工作。

当点击“pressedClassicButton”时,它会在其他视图中重新加载一些数据并更改其他一些uiviews的图像。它不应该以任何方式触摸imageView。它更改的数据也没有连接到imageView。

但不知何故,当我调用“pressedClassicButton”方法时,它开始忽略我指定的ipad帧大小,并设置storyboard约束上指定的imageView大小。这个问题只发生在ipad中。

约束条件是:1:1比率,20px到左右和顶部。

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

1 个答案:

答案 0 :(得分:1)

使用约束并尝试设置框架时会发生意外情况。请尝试通过更改其约束来设置视图的大小。您可以通过按住Ctrl键单击拖动来将它们添加为故事板中的属性,就像连接按钮时一样。