尝试将动画部分放在控制器类而不是子类按钮类中。
//in customButton.h file
#import <UIKit/UIKit.h>
@interface customButton : UIButton
- (id)initWithFrameOfCustomButton:(CGRect)frame;
- (void)animate;//this method u need to add and call it from the controller
@end
//in the subclassed UIButtin class
//in customButton.m file
#import "customButton.h"
#import <QuartzCore/QuartzCore.h>
@implementation customButton
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithFrameOfCustomButton:(CGRect)frame
{
self = [super initWithFrame:frame];
if(self)
{
//hear only set the frame only not incude animation part
//this is the custom initiliser that initilises the custom button with the given frame and also make it to round
self.frame = frame;
self.backgroundColor = [UIColor greenColor];
self.layer.cornerRadius = frame.size.width/2;
self.layer.masksToBounds = YES;
}
return self;
}
//add the animation part
- (void)animate //this method is called within the this class
{
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f);
//edit: comment below lines becz u already made it to round
// self.layer.cornerRadius = 0.0f;
// self.layer.masksToBounds = YES;
// self.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(0 , 0, 1.0f);
// cButton.layer.transform = t;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
self.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
// edit comment below lines
// self.layer.cornerRadius = self.frame.size.width/2;
// self.layer.masksToBounds = YES;
// self.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(1 , 1, 1.0f);
// cButton.layer.transform = t;
} completion:nil];
}];
}
//implement touch handling methods to call animation
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self animate];
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self animate];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self animate];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
// [self animate];
}
在控制器类中包括如下动画部分
//in the view controller that u are using this custom button
#import "FirstViewController.h"
#import "CustomCell.h"
#import "customButton.h"
#import <QuartzCore/QuartzCore.h>
@interface FirstViewController ()
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
customButton *cButton = [[customButton alloc]initWithFrameOfCustomButton:CGRectMake(20, 30, 100, 100)]; //hear i am initilising the custom button
cButton.tag = 100;//assigning tag to access during the animation
[self.view addSubview:cButton];
// [self makeAnimation];//i am animation rite after it loads the all views u can place this method call where ever u want
//edit: i am commenting the this line so animations of the button in this class won't call
}
//edit below method is added
- (void)viewDidAppear:(BOOL)animated
{
customButton *cButton = (customButton *)[self.view viewWithTag:100];
//edit->comment this line so that u never call the animation from view controller
// [cButton animate];//animating the button the code in the subclassed method is called
}
- (void)makeAnimation
{
customButton *cButton = (customButton *)[self.view viewWithTag:100];//get the custom button by using the tag
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
cButton.layer.affineTransform = CGAffineTransformMakeScale(0.0f, 0.0f);
// cButton.layer.cornerRadius = 0.0f;
// cButton.layer.masksToBounds = YES;
// cButton.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(0 , 0, 1.0f);
// cButton.layer.transform = t;
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{
cButton.layer.affineTransform = CGAffineTransformMakeScale(1.0f, 1.0f);
// cButton.layer.cornerRadius = cButton.frame.size.width/2;
// cButton.layer.masksToBounds = YES;
// cButton.clipsToBounds = YES;
// CATransform3D t = CATransform3DIdentity;
// t = CATransform3DMakeScale(1 , 1, 1.0f);
// cButton.layer.transform = t;
} completion:nil];
}];
}
注意 UIButton 继承自: UIControl -> UIView -> UIResponder -> NSObject 如果您感到困惑,请查看文档如果您想使用 UIControlers 即响应程序,那么您需要实现这些方法,您可以获得您想要的事件,
在您的子类 UIButton 类中通过注释触摸处理方法来实现这一点
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}
- (void)endTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
}
- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(UIEvent *)event
{
return YES;
}