如何让图像在触摸时改变它的alpha值

时间:2012-10-01 19:54:55

标签: ios cocoa-touch alpha uicontrol

该按钮不是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];

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

我被困住了。我无法弄清楚如何参考中心图像。

这是我的.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或其他东西)

1 个答案:

答案 0 :(得分:1)

要更改使用字母: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界面。