#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[RootViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
@end
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
#import "RootViewController.h"
@interface RootViewController ()
{
CGFloat buttonScale;//比例
}
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
//初始化button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
buttonScale = 1.0;
button.frame = CGRectMake(100, 100, 120, 60);
[button setTitle:@"按钮" forState:0];
[button setBackgroundImage:[UIImage imageNamed:@"button"] forState:0];
[button addTarget:self action:@selector(buttonDownAction:) forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
[self.view addSubview:button];
}
/**
* 按钮按下时,执行的方法
*/
- (void)buttonDownAction:(UIButton*)sender{
CGFloat scale = buttonScale < 1.0 ? 1.0 : 0.9;
//变小
[UIView animateWithDuration:0.25 animations:^{
sender.transform = CGAffineTransformMakeScale(scale, scale);
}];
NSLog(@"变小");
}
/**
* 松开按钮时,执行的方法
*/
- (void)buttonAction:(UIButton*)sender{
//恢复原来的尺寸
[UIView animateWithDuration:0.25 animations:^{
sender.transform = CGAffineTransformMakeScale(1.0, 1.0);
} completion:^(BOOL finished) {
//在此执行相应操作
NSLog(@"恢复");
}];
}
@end