UIImageView 触摸事件

UIImageView Touch Event(UIImageView 触摸事件)
本文介绍了UIImageView 触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在触摸名为(image1)的uiimageview时调用我的按钮单击事件.放置在uiview(view1)中的Imageview.

这是我的代码:

- (IBAction)buttonClicked:(id)sender{myTimer = [NSTimer scheduleTimerWithTimeInterval:2.0 target:self 选择器:@selector(alphabutt:) userInfo:nil repeats:NO];}-(IBAction)alphabutt:(id)发件人{NSLog(@"alphabutt!");如果(我==1){NSLog(@"i==1");[image1 setImage:[UIImage imageNamed:@"appleimg.jpg"]];[view1 addSubview:image1];transition1 = [CATransition 动画];过渡1.duration = 0.75;transition1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];transition1.type=kCATransitionReveal;transition1.subtype=kCATransitionFromRight;过渡=是;transition1.delegate = self;[image1.layer addAnimation:transition1 forKey:nil];}如果(我==2){NSLog(@"i==2");[image1 setImage:[UIImage imageNamed:@"boatimg.jpg"]];[view1 addSubview:image1];transition2 = [CATransition 动画];过渡2.duration = 0.75;transition2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];transition2.type=kCATransitionFade;transition2.subtype=kCATransitionFromRight;过渡=是;transition2.delegate = self;[image1.layer addAnimation:transition2 forKey:nil];我=0;}我++;}

上面是我在单击按钮时调用 buttonclicked 事件的代码.在 buttonclick 事件中,计时器正在以 2.0 的时间间隔调用 alphabutt(按钮单击事件).每隔 2.0 调用一次 alphabutt.我想要当我触摸 uiimageview(image1) 时执行此动画,然后它只会调用按钮单击事件 (alphabutt).如何为 imageview 编写触摸事件...

请用一些代码简单解释一下执行uiimageview触摸事件...

解决方案

可以使用UITapGestureRecognizer通过addGestureRecognizer添加到UIImageViewp>

片段:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];singleTap.numberOfTapsRequired = 1;singleTap.numberOfTouchesRequired = 1;[iv addGestureRecognizer:singleTap];[iv setUserInteractionEnabled:YES];

- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {NSLog(@"%@", [gestureRecognizer view]);}

I want to call my button click event while touching the uiimageview named as(image1).Imageview placed in uiview(view1).

This is my code:

- (IBAction)buttonClicked:(id)sender
{

 myTimer = [NSTimer scheduledTimerWithTimeInterval:2.0 target:self selector:@selector(alphabutt:) userInfo:nil repeats:NO];
 }


-(IBAction)alphabutt:(id)sender
{
 NSLog(@"alphabutt!");


 if(i==1)
 {
  NSLog(@"i==1");
  [image1 setImage:[UIImage imageNamed:@"appleimg.jpg"]];
  [view1 addSubview:image1];  
  transition1 = [CATransition animation];

  transition1.duration = 0.75;

  transition1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

  transition1.type=kCATransitionReveal;
  transition1.subtype=kCATransitionFromRight;
  transitioning = YES;
  transition1.delegate = self;
  [image1.layer addAnimation:transition1 forKey:nil];  
 }
 if(i==2)
 {
  NSLog(@"i==2");
  [image1 setImage:[UIImage imageNamed:@"boatimg.jpg"]];
  [view1 addSubview:image1];  
  transition2 = [CATransition animation];

  transition2.duration = 0.75;

  transition2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];

  transition2.type=kCATransitionFade;
  transition2.subtype=kCATransitionFromRight;
  transitioning = YES;
  transition2.delegate = self;
  [image1.layer addAnimation:transition2 forKey:nil];

  i=0;
 }
i++;

}

The above is my code while clicking the button the buttonclicked event is called.Inside buttonclick event the timer is working to call the alphabutt(Button click event)with the time interval of 2.0.alphabutt is called with every 2.0.I want to do this animation when i'm touch the uiimageview(image1) then only it calls the button click event (alphabutt). how can I write the touch event for imageview...

Please explain briefly with some code to perform uiimageview touch event...

解决方案

you can use UITapGestureRecognizer added to the UIImageView via addGestureRecognizer

snippets:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[iv addGestureRecognizer:singleTap];
[iv setUserInteractionEnabled:YES];

and

- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
    NSLog(@"%@", [gestureRecognizer view]);
}

这篇关于UIImageView 触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
java.lang.IllegalStateException: SimpleTypeImpl should not be created for error type(异常:不应为错误类型创建SimpleTypeImpl)
Android IllegalArgumentException: The tag for fragment_XXX is invalid. Received: layout-sw600dp/fragment_XXX_0(Android IlLegalArgumentException:Fragment_XXX的标签无效。收到:Layout-sw600dp/Fragment_XXX_0)
iOS convert audio sample rate from 16 kHz to 8 kHz(IOS将音频采样率从16 kHz转换为8 kHz)
Enforcing an audio sampling rate in iOS(在iOS中强制音频采样率)
HTTPS request using volley(使用 volley 的 HTTPS 请求)