我想在点按时将UIbutton
淡入其突出显示的状态,然后在用户从按钮上移开手指时淡出。我尝试使用transitionWithView
执行此操作,但这仅适用于当用户移除其手指时淡出突出显示状态。
这是我的代码。感谢。
[UIView transitionWithView:view duration:.2 options:UIViewAnimationOptionTransitionCrossDissolve | UIViewAnimationOptionAllowUserInteraction animations:nil completion:nil];
答案 0 :(得分:2)
首先制作一个新的UIButton子类,在我的例子中我称之为" MyButton"。
并使用:
的touchesBegan
touchesEnded
touchesMoved
为了实现您正在寻找的淡入淡出,请执行以下操作:
#import <UIKit/UIKit.h>
@interface MyButton : UIButton
@end
#import "MyButton.h"
@implementation MyButton
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration:0.2 animations:^{
self.viewForBaselineLayout.alpha = 0;
}];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration:0.2 animations:^{
self.viewForBaselineLayout.alpha = 1;
}];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[UIView animateWithDuration:0.2 animations:^{
self.viewForBaselineLayout.alpha = 1;
}];
}
@end
最后在你的UIVIewController中使用它:
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
MyButton *button = [[MyButton alloc]init];
[button setFrame:CGRectMake(100, 100, 100, 100)];
[button setTintColor:[UIColor blackColor]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitle:@"HelloThere" forState:UIControlStateNormal];
[self.view addSubview:button];
}
答案 1 :(得分:-3)
你试过这个吗?
[UIView animateWithDuration:0.2 animations:^{
button.highlighted = YES;
}];