示例代码
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void) createImageBtn
{
UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
[imageView setImage:[UIImage imageNamed:@"7.jpg"]];
imageView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"3.jpg"], [UIImage imageNamed:@"2.jpg"],[UIImage imageNamed:@"7.jpg"], nil];
imageView.animationDuration = 0.3f;
imageView.animationRepeatCount = 0;
[imageView startAnimating];
[self.view addSubview:imageView];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createImageBtn];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void) createBtn
{
UIButton* btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
UIButton* btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.tag = 100;
btn1.tag = 101;
btn.frame = CGRectMake(100, 100, 80, 40);
[btn setTitle:@"按钮" forState:UIControlStateNormal];
btn.frame = CGRectMake(100, 140, 80, 40);
[btn setTitle:@"按钮" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(showSheet) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
- (IBAction)showAlert:(UIButton*)sender
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"注册" message:@"表面注册了解一下" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yes = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction* no = [UIAlertAction actionWithTitle:@"返回" style:UIAlertActionStyleDestructive handler:nil];
[alert addAction:yes];
[alert addAction:no];
[self presentViewController:alert animated:YES completion:nil];
}
- (IBAction)showSheet:(UIButton* )sender
{
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"1" message:@"2" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"3" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction* deleteAction = [UIAlertAction actionWithTitle:@"4" style:UIAlertActionStyleDestructive handler:nil];
UIAlertAction* apple = [UIAlertAction actionWithTitle:@"5" style:UIAlertActionStyleDefault handler:nil];
UIAlertAction* banana = [UIAlertAction actionWithTitle:@"6" style:UIAlertActionStyleDefault handler:nil];
[alert addAction:cancelAction];
[alert addAction:apple];
[alert addAction:banana];
[alert addAction:deleteAction];
[alert presentViewController:alert animated:YES completion:nil];
}
- (void) pressBtn:(UIButton* ) btn
{
if (btn.tag == 100) {
[self showSheet:btn];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createBtn];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
心得体会
- 记住要把他放到屏幕上