【问题标题】:how to you make an image change it's alpha when touched如何让图像在触摸时更改为 alpha
【发布时间】:2012-10-01 19:54:54
【问题描述】:

按钮不是UIButton,它是UIControl的子类

当用户点击我的UIControl 时,我想更改 alpha,以便用户有一些视觉反馈,表明它被按下了。然后放开再改

UIImageView *mask =[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 58, 58)];
mask.image =[UIImage imageNamed:@"centerButton.png"];
mask.center = self.center;
mask.center = CGPointMake(mask.center.x, mask.center.y+3);
[self addSubview:mask];

中心按钮是使用rayWenderlich.com 上的a tutorial 创建的,用于了解如何创建自定义控件。

我被困住了。我不知道如何引用中心图像。

这是我的 .h 类

@interface OOTRotaryWheel : UIControl


@property (weak) id <OOTRotaryProtocol> delegate;
@property (nonatomic, strong) UIView *container;
@property int numberOfSections;
@property CGAffineTransform startTransform;
@property (nonatomic, strong) NSMutableArray *sectors;
@property int currentSector;
@property (nonatomic, retain) UIImageView *mask;

- (id) initWithFrame:(CGRect)frame andDelegate:(id)del withSections:(int)sectionsNumber;
- (void)rotate;
- (void) buildSectorsEven;
- (void) buildSectorsOdd;

@end

那么我如何检测 UIImageView 上的触碰和触碰?让它更像一个按钮(这样当我可以加载另一个 xib 或其他东西时)

【问题讨论】:

    标签: ios cocoa-touch alpha uicontrol


    【解决方案1】:

    更改 alpha 使用:mask.alpha = 0.0f;

    使用IBActions
    按下按钮时采取行动:

    -(IBAction)buttonPressedDown{
          mask.alpha = 0.5f;
    }//Connect this IBAction to touchDown
    

    要将按钮改回原来的 Alpha:

    -(IBAction)buttonUpInsideAndOutside{
          mask.alpha = 1.0f;
    }//Connect this IBAction to touchUpInside and touchUpOutside
    

    使用UITouches
    按下按钮时采取行动:

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
          UITouch*touch = [touches anyObject];
          if(touch.view==mask)
                mask.alpha = 0.5f;
    }//Will only work if the IBOutlet is connected
    

    要将按钮改回原来的 Alpha:

    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
          mask.alpha = 1.0f;
    }//Will only work if the IBOutlet is connected
    

    不管你是否有 xib/storyboard 界面,这都会起作用。

    【讨论】:

    • 我认为您的意思是“touchUpInside”而不是“buttonUpInside”。
    • 是的,但是 Xib 是空的。控件是在代码中创建的。 (请查看该网站,因为我遵循了教程)。我在属性中将 Mask 设为变量
    • 如果掩码变量不是全局变量,请将其设为全局变量,因为这是最有效的方法。否则,您将使用这个很酷的高级 for 循环,但效率不高,请尝试 UITouches 方法,它应该可以工作,如果不行,我将向您展示您的其他 2 个选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多