简体   繁体   English

UIButton只能部分轻按

[英]UIButton only partially tappable

So, this seems like a weird occurrence. 因此,这似乎是一个奇怪的现象。 I have a UIButton with a custom background image and and animation that is called randomly (it's an image of a buoy rocking back and forth). 我有一个带有自定义背景图像和动画的UIButton,它被随机调用(这是一个来回摇摆的浮标的图像)。 The strange thing is, only the bottom portion of the button is tappable. 奇怪的是,只有按钮的底部是可点击的。 I have another button that is identical in the way that is set up, and you can tap anywhere on the image. 我还有另一个按钮,其设置方式相同,您可以点击图像上的任意位置。

The background image is just a png (it has transparency) but the other buttons I've created are work as long as you tap within the bounding box. 背景图像只是png(具有透明度),但是只要您在边界框内点击,我创建的其他按钮就可以使用。 Any ideas? 有任何想法吗?

-(void)displayPauseButton
{
    NSArray *pauseInfo = [self.sharedGameModel.theAssets objectForKey:@"buoy-ani"];

    //
    // add mascot button
    // aButtonArray has the info for a individual button in an array
    // the buttons are read from the array in this order
    // 0 array of images for the button animation triggered when clicked
    // 1 x position
    // 2 y position

    // this array only has the names of the image animation
    // so we init
    NSArray *pauseImages = [pauseInfo objectAtIndex:0];
    UIImage *pauseImage = [UIImage imageNamed:[pauseImages objectAtIndex:0]];
    CGFloat theX=[[pauseInfo objectAtIndex:1] floatValue];
    CGFloat theY=[[pauseInfo objectAtIndex:2] floatValue];

    // create a new mutable array to fill with the actual object
    // in the following loop
    NSMutableArray *buttonAniImages = [[NSMutableArray alloc] init];
    for (id object in pauseImages)
    {
        [buttonAniImages addObject:[UIImage imageNamed:object]];
    }

    // sets the custom image for the button
    [self.pauseButton setImage:pauseImage forState:UIControlStateNormal];

    self.pauseButton.frame = CGRectMake(theX, theY, pauseImage.size.width, pauseImage.size.height);
    self.pauseButton.imageView.animationImages = buttonAniImages;

    self.pauseButton.imageView.animationDuration=3.0;
    self.pauseButton.imageView.animationRepeatCount=1;

    [self.gameScreen addSubview:self.pauseButton];

    // add the action associated with the button
    [self.pauseButton addTarget:self action:@selector(someoneHitThePauseButton:) forControlEvents:UIControlEventTouchUpInside];
}

Escrafford is right. Escrafford是正确的。 There was something on top, only it was never visibly on top. 有东西在上面,只有它从来没有明显地在上面。 I'll add my experience just in case anyone else runs into the same issue. 我将补充我的经验,以防其他人遇到相同的问题。

In my game, there are multiple UIViews that have path animations. 在我的游戏中,有多个具有路径动画的UIView。 Their initial frames were initially set to 0,0 and then the path starting points were adjusted randomly. 最初将它们的初始帧设置为0,0,然后随机调整路径起点。 But their initial frames were covering the top portion of the buttons. 但是它们的初始框架覆盖了按钮的顶部。

Thanks to everyone who provided feedback. 感谢所有提供反馈的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM