iOS UIImageView的简单使用

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    UIImageView *pImageView=[[UIImageView alloc]initWithFrame:CGRectMake(20, 110, 280,200 )];
    pImageView.image=[UIImage imageNamed:@"p3"];
    pImageView.tag=100;
    //是否高亮
//    pImageView.highlighted=YES;
    //当添加按钮时 必须YES
    pImageView.userInteractionEnabled=YES;
    //背景色
    pImageView.backgroundColor=[UIColor redColor];
    //高亮时的图片
    pImageView.highlightedImage=[UIImage imageNamed:@"p4.png"];
    //添加视图
    [self.window addSubview:pImageView];
    
    //创建Button 将它添加在imageView上
    UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame=CGRectMake(105, 80, 70, 40);
    [btn setTitle:@"start" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(changeShow:) forControlEvents:UIControlEventTouchUpInside];
    //将按钮添加到 UIImageView 上
    //添加按钮则   pImageView.userInteractionEnabled=YES; 必须写上
    [pImageView addSubview:btn];
    
    return YES;
}

-(void)changeShow:(id)sender
{
    static BOOL num=YES;
    //获取 UIImageView
     UIImageView *pImageView=(UIImageView *)[self.window viewWithTag:100];
    if (num) {
        pImageView.highlighted=YES;
        num=NO;
    }
    else
    {
        pImageView.highlighted=NO;
        num=YES;
        
    }
}

你可能感兴趣的:(iOS UIImageView的简单使用)