2014-08-28 55 views
0

我正在使用一种方法向我的按钮添加阴影。问题出在那之后,我不能点击它们。我将userinteractionenabled属性添加到YES,但仍然无法点按。添加子视图后无法点击UIButton

这是代码。

我该如何解决?我错过了什么吗?

- (UIView*)putView:(UIView*)view insideShadowWithColor:(UIColor*)color andRadius:(CGFloat)shadowRadius andOffset:(CGSize)shadowOffset andOpacity:(CGFloat)shadowOpacity 
{ 
    CGRect shadowFrame; // Modify this if needed 
    shadowFrame.size.width = 0.f; 
    shadowFrame.size.height = 0.f; 
    shadowFrame.origin.x = 0.f; 
    shadowFrame.origin.y = 0.f; 
    UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame]; 
    shadow.userInteractionEnabled = YES; // Modify this if needed 
    shadow.layer.shadowColor = color.CGColor; 
    shadow.layer.shadowOffset = shadowOffset; 
    shadow.layer.shadowRadius = shadowRadius; 
    shadow.layer.masksToBounds = NO; 
    shadow.clipsToBounds = NO; 
    shadow.layer.shadowOpacity = shadowOpacity; 
    [view.superview insertSubview:shadow belowSubview:view]; 
    [shadow addSubview:view]; 
    return shadow; 
} 
+0

你的影子框架是0,0,0,0,除非你张贴不正确的代码... – rebello95 2014-08-28 02:42:56

回答

0

这是有道理的,因为你已经把一个UIView上的按钮,这意味着你不能访问该按钮的上方。您已在UIView上设置UserInteractionEnabled,但这没用,因为您并未要求UIView响应触摸事件。在这种情况下,我会建议在UIView中添加一个UIGestureRecognizer,而不是试图将一个UIView放在按钮上。

+0

所以,我需要添加一个手势到阴影视图?我的IBAction将无用? – 2014-08-28 02:56:11

+0

正如你现在建立它,IBAction是无用的。想想看...在视图层次结构中,你已经将一个UIView放置在UIButton的顶部,因此用户无法访问位于视图下方的按钮。您可以尝试的一种解决方法是在阴影视图上使用sendSubViewToBack,并使UIButton背景色透明,并且应该具有所需的效果。 UIButton将位于顶部,并且通过它可以看到阴影视图 – cph2117 2014-08-28 03:01:33

+0

我正在尝试添加该行[shadow bringSubviewToFront:view];但它不起作用。另外,我添加了[shadow sendSubviewToBack:view],但不起作用。 移动子视图的最佳方法是什么? – 2014-08-28 03:23:41

0
CGRect shadowFrame; // Modify this if needed 
shadowFrame.size.width = 0.f; 
shadowFrame.size.height = 0.f; 
shadowFrame.origin.x = 0.f; 
shadowFrame.origin.y = 0.f; 
UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame]; 

在上面的代码中,您有一个视图与帧0,0,0,0。那么你怎么能点击它?

+0

好的,我明白了你的观点。我将其更改为此 CGRect shadowFrame; //如果需要,修改它 shadowFrame.size.width = view.frame.size.width; shadowFrame.size.height = view.frame.size.height; shadowFrame.origin.x = 0.f; shadowFrame.origin.y = 0.f; 并将阴影视图背景更改为绿色以查看其位置。现在它位于左上角。但是如果我改变原点,其他元素就会改变它们的位置。我该如何解决这个问题? – 2014-08-28 03:50:26

1

我找到解决方案

经过人们的评论,我做了一些改变。这是最终的代码

+ (void)putView:(UIView*)view insideShadowWithColor:(UIColor*)color andBlur:(CGFloat)shadowRadius andOffset:(CGSize)shadowOffset andOpacity:(CGFloat)shadowOpacity 
{ 
    CGRect shadowFrame = view.frame; 
    UIView * shadow = [[UIView alloc] initWithFrame:shadowFrame]; 
    shadow.backgroundColor = [UIColor redColor]; 
    shadow.userInteractionEnabled = YES; // Modify this if needed 
    shadow.layer.shadowColor = color.CGColor; 
    shadow.layer.shadowOffset = shadowOffset; 
    shadow.layer.shadowRadius = shadowRadius; 
    shadow.layer.cornerRadius = view.layer.cornerRadius; 
    shadow.layer.masksToBounds = NO; 
    shadow.clipsToBounds = NO; 
    shadow.layer.shadowOpacity = shadowOpacity; 
    [view.superview insertSubview:shadow belowSubview:view]; 
} 

有了这个,你可以同时具有圆角和阴影的视图。而且,当然,触摸事件启用!

相关问题