2012-10-01 9 views
0

按鈕是不是UIButton時的α,它是UIControl怎麼給你使圖像改變它觸及

一個子類,當用戶點擊我的UIControl,我想改變阿爾法,使用戶有一定的視覺反饋,它被按下。然後在rayWenderlich.com發佈

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創建學習有關創建自定義的控件時,再次進行更改。

我被卡住了。我無法弄清楚如何引用中心圖像。

這裏是我的.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和觸摸起來?並使它更像一個按鈕(所以,當我可以加載另一個廈門國際銀行或某事)

回答

1

要更改字母的使用:mask.alpha = 0.0f;

要使用IBActions做到這一點:
要採取行動時,該按鈕按:

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

更改按鈕返回到它的原始阿爾法:

-(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 

更改按鈕返回到它的原始阿爾法:

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

這將工作,無論你有沒有xib/storyboard界面。

+0

我認爲你的意思是「touchUpInside」而不是「buttonUpInside」。 – pasawaya

+0

是的,但Xib是空的。這些控件是在代碼中創建的。 (請參閱網站,因爲我遵循教程)。我在屬性 –

+0

中爲Mask創建了一個變量。如果mask變量不是全局變量,則將其變爲全局變量,因爲這將是最有效的方法。否則,你會使用這個酷的高級for循環,但它不是那麼高效,嘗試UITouches方法,它應該工作,如果沒有,我會告訴你另外兩個選項。 – Comradsky