UI04_delegate

本文详细介绍了如何在iOS应用中创建窗口和视图控制器,并配置其基本属性,包括背景颜色和视图添加。同时,展示了如何通过协议实现视图交互和图像显示,以及使用模糊效果增强用户体验。

AppDelegate.h

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "RootViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate
- (void)dealloc
{
    [_window release];
    [super dealloc];
}

- (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 makeKeyAndVisible];
    [_window release];

    RootViewController *rootVC = [[RootViewController alloc] init];
    self.window.rootViewController = rootVC;
    [rootVC release];

    return YES;
}

RootViewController.h

#import <UIKit/UIKit.h>

@interface RootViewController : UIViewController

@end

RootViewController.m

#import "RootViewController.h"
#import "MyButton.h"
//  4. 签协议
@interface RootViewController ()<MyButtonDelegate>

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.view.backgroundColor = [UIColor whiteColor];

//    //  创建一个mybutton对象
//    MyButton *button = [[MyButton alloc] initWithFrame:CGRectMake(100, 100, 150, 50)];
//    button.backgroundColor = [UIColor yellowColor];
//    [self.view addSubview:button];
//    [button release];
//    //  5. 设置代理人
//    button.delegate = self;
//    
//    //  <>里面放两个#号, 可用tap选择

    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 200)];
    //  给imageView设置图片
    imageView.image = image;
    [self.view addSubview:imageView];
    [imageView release];

//    imageView.layer.cornerRadius = 100;
//    imageView.layer.borderWidth = 1;
//    imageView.layer.masksToBounds = YES;

    //  毛玻璃效果
    UIBlurEffect *effect = [UIBlurEffect effectWithStyle: UIBlurEffectStyleLight];
    //  iOS8.0之后出现的新效果, 用来显示模糊效果
    UIVisualEffectView *effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    effectView.frame = CGRectMake(0, 0, imageView.frame.size.width, imageView.frame.size.height);
    effectView.alpha = 0.8;
    //  添加子视图
    [imageView addSubview:effectView];
    [effectView release];


}
//  6. 实现协议方法
- (void)changeColor {
    self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}

MyButton.h

#import <UIKit/UIKit.h>

//  1. 声明协议
@protocol MyButtonDelegate <NSObject>

- (void)changeColor;

@end

@interface MyButton : UIView

//  声明代理人的属性
@property(nonatomic, assign)id<MyButtonDelegate>delegate;


@end

MyButton.m

#import "MyButton.h"

@implementation MyButton

//  3. 通过touch方法, 来设置代理人执行的方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.delegate changeColor];
}

@end
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值