在带有子视图的 UIButton 上显示突出显示

发布于 2024-12-11 19:36:08 字数 1174 浏览 0 评论 0原文

我正在尝试创建一个具有以下特征的 UIbutton: * 自定义图像作为其内容 * 圆角 * 投影

使用以下代码将格式化按钮以正确显示并正常工作,但在发生触摸时突出显示操作(即,触摸时按钮变暗)会丢失。有什么想法吗?

+(void) styleButton:(UIButton*)button{
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(button.bounds.origin.x, button.bounds.origin.y, button.bounds.size.width, button.bounds.size.height)];    

    CALayer *sublayer = [CALayer layer];
    sublayer.shadowOffset = CGSizeMake(0, 3);
    sublayer.shadowRadius = 5.0;
    sublayer.shadowColor = [UIColor blackColor].CGColor;
    sublayer.shadowOpacity = 0.8;

    sublayer.frame = CGRectMake(button.bounds.origin.x, button.bounds.origin.y, button.bounds.size.width, button.bounds.size.height);

    backgroundView.userInteractionEnabled = NO;

    [backgroundView.layer addSublayer:sublayer];

    [button insertSubview:backgroundView atIndex:0];

    CALayer *imageLayer = [CALayer layer];
    imageLayer.frame = sublayer.bounds;
    imageLayer.cornerRadius = 10.0;
    imageLayer.contents = (id) [UIImage imageNamed:@"myImage.png"].CGImage;
    imageLayer.cornerRadius = 5.0f;
    imageLayer.masksToBounds = YES;
    [sublayer addSublayer:imageLayer];
}

I'm trying to create a UIbutton that has the following characteristics:
* A custom image as its content
* Rounded corners
* A drop shadow

Using the following code will format the button to appear correctly and function but the highlight action (i.e., the darkening of the button when touched) is lost when the touch occurs. Any ideas?

+(void) styleButton:(UIButton*)button{
    UIView *backgroundView = [[UIView alloc] initWithFrame:CGRectMake(button.bounds.origin.x, button.bounds.origin.y, button.bounds.size.width, button.bounds.size.height)];    

    CALayer *sublayer = [CALayer layer];
    sublayer.shadowOffset = CGSizeMake(0, 3);
    sublayer.shadowRadius = 5.0;
    sublayer.shadowColor = [UIColor blackColor].CGColor;
    sublayer.shadowOpacity = 0.8;

    sublayer.frame = CGRectMake(button.bounds.origin.x, button.bounds.origin.y, button.bounds.size.width, button.bounds.size.height);

    backgroundView.userInteractionEnabled = NO;

    [backgroundView.layer addSublayer:sublayer];

    [button insertSubview:backgroundView atIndex:0];

    CALayer *imageLayer = [CALayer layer];
    imageLayer.frame = sublayer.bounds;
    imageLayer.cornerRadius = 10.0;
    imageLayer.contents = (id) [UIImage imageNamed:@"myImage.png"].CGImage;
    imageLayer.cornerRadius = 5.0f;
    imageLayer.masksToBounds = YES;
    [sublayer addSublayer:imageLayer];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆流 2024-12-18 19:36:08

您是否尝试过将backgroundView转换为UIImage并将其添加到按钮而不是将backgroundView添加为子视图?这将在按下按钮时保持变暗:

UIGraphicsBeginImageContext(backgroundView.frame.size);
[backgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[button setImage:image forState:UIControlStateNormal];

Have you tried converting your backgroundView into a UIImage and add it to the button instead of adding backgroundView as a subview? That would preserve the darkening when the button is pressed:

UIGraphicsBeginImageContext(backgroundView.frame.size);
[backgroundView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[button setImage:image forState:UIControlStateNormal];
情深已缘浅 2024-12-18 19:36:08

像这样(启用 ARC):

UIGraphicsBeginImageContextWithOptions(self.button.bounds.size, NO, [[UIScreen mainScreen] scale]);

CGContextRef context = UIGraphicsGetCurrentContext();

for (UIView *aView in self.button.subviews) {

    CGContextSaveGState(context);
    CGContextTranslateCTM(context, aView.frame.origin.x, aView.frame.origin.y);
    [aView.layer renderInContext:UIGraphicsGetCurrentContext()];
    CGContextRestoreGState(context);
}

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[self.button setImage:image forState:UIControlStateNormal];
[self.button.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.button setAdjustsImageWhenHighlighted:YES];

Like this (ARC enabled):

UIGraphicsBeginImageContextWithOptions(self.button.bounds.size, NO, [[UIScreen mainScreen] scale]);

CGContextRef context = UIGraphicsGetCurrentContext();

for (UIView *aView in self.button.subviews) {

    CGContextSaveGState(context);
    CGContextTranslateCTM(context, aView.frame.origin.x, aView.frame.origin.y);
    [aView.layer renderInContext:UIGraphicsGetCurrentContext()];
    CGContextRestoreGState(context);
}

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

[self.button setImage:image forState:UIControlStateNormal];
[self.button.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
[self.button setAdjustsImageWhenHighlighted:YES];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文