您的位置:首页 > 产品设计 > UI/UE

UIView UIButton UIImageView 添加事件

2015-06-02 22:11 615 查看
UIButton 添加事件   

 UIButton *aButton = [UIButton
buttonWithType:UIButtonTypeCustom];
    aButton.frame =
CGRectMake(50,
50, 80,
40);

    [aButton setBackgroundColor:[UIColor
grayColor]];
    [aButton
setAlpha:0.4];
    [aButton.layer
setMasksToBounds:YES];
    [aButton.layer
setCornerRadius:10.0];
//设置矩形四个圆角半径
    [aButton.layer
setBorderWidth:1.0];
//边框宽度

//添加事件的代码 使用UITapGestureRecognizer, 需要实现的是doImgClick

    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer
alloc]
initWithTarget:self
action:@selector(doImgClick)];
//加入到aImageView上。
    [aImageView
addGestureRecognizer:singleTap];
    [aView
addSubview:aButton];

实现 doImgClick

- (void)doClick:(UIButton *)button
{

    NSLog(@"do Click  = %@", button);

    NSString *title = [button
titleForState:UIControlStateNormal];

    NSLog(@"After Click = %@", title);
}

给图片添加事件,首先要在UIImageView上添加一个图片

    UIImageView *bImageView = [[UIImageView
alloc] initWithFrame:CGRectMake(30,
240, 60,
60)];

    [bImageView setBackgroundColor:[UIColor
greenColor]];
    [bImageView
setAlpha:0.8];
    [bImageView
setHighlighted:YES];

    [bImageView setHighlightedImage:[UIImage
imageNamed:@"0.png"]];

    [bImageView setUserInteractionEnabled:YES];

//添加事件代码

    UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer
alloc]
initWithTarget:self
action:@selector(doImgClick)];
    [bImageView
addGestureRecognizer:singleTap1];
    [bImageView
addSubview: aImageView];

实现 doImgClick 该方法是点击后,调到另外一个UIView上。

- (void)doImgClick
{
   
UIView *aView = [[UIView
alloc] initWithFrame:CGRectMake(0,
0, 400,
500)];

    [aView setBackgroundColor:[UIColor
greenColor]];

    [aView setClipsToBounds:YES];
    [self.window
addSubview:aView];

    
   
UIImageView *aImageView = [[UIImageView
alloc] initWithFrame:CGRectMake(0,
0, 400,
500)];

    [aImageView setBackgroundColor:[UIColor
greenColor]];

    //[aImageView setAlpha:0.8];
    [aImageView
setHighlighted:YES];

    [aImageView setHighlightedImage:[UIImage
imageNamed:@"a.png"]];

   
UIImageView *bImageView = [[UIImageView
alloc] initWithFrame:CGRectMake(0,
0, 60,
60)];

    [bImageView setBackgroundColor:[UIColor
greenColor]];
    [bImageView
setHighlighted:YES];

    [bImageView setHighlightedImage:[UIImage
imageNamed:@"0.png"]];

    [bImageView setUserInteractionEnabled:YES];
//这里我实现了一个 跳到另一个UIView的方法 

    UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer
alloc]
initWithTarget:self
action:@selector(doImg1Click)];
    [bImageView
addGestureRecognizer:singleTap1];
    [aView
addSubview:aImageView];
    [aView
addSubview:bImageView];

    

    NSLog(@"do Click");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: