我想在触摸名为(image1)的uiimageview时调用我的按钮单击事件.Imageview放在uiview(view1)中.
这是我的代码:
- (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++;
}
Run Code Online (Sandbox Code Playgroud)
以上是我的代码,同时点击按钮,调用buttonclicked事件.内部按钮点击事件计时器正在工作,调用alphabutt(按钮点击事件),时间间隔为2.0.alphabutt每2.0调用.我想这样做动画当我触摸uiimageview(image1)时,它只调用按钮点击事件(alphabutt).如何为imageview编写触摸事件...
请简要介绍一些代码来执行uiimageview触摸事件......
Tek*_*Yin 61
你可以使用UITapGestureRecognizer添加到UIImageView通道addGestureRecognizer
片段:
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bannerTapped:)];
singleTap.numberOfTapsRequired = 1;
singleTap.numberOfTouchesRequired = 1;
[iv addGestureRecognizer:singleTap];
[iv setUserInteractionEnabled:YES];
Run Code Online (Sandbox Code Playgroud)
和
- (void)bannerTapped:(UIGestureRecognizer *)gestureRecognizer {
NSLog(@"%@", [gestureRecognizer view]);
}
Run Code Online (Sandbox Code Playgroud)
Swa*_*uke 57
在xib中启用图像视图用户交互(如果以编程方式添加,则通过代码),并使用UIResponder方法touchesBegan,touchesMoved,touchesEnded等来检测图像视图上的触摸:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == yourImageView)
{
//add your code for image touch here
}
}
Run Code Online (Sandbox Code Playgroud)
Kin*_*ard 13
在这里,我向您展示在Swift中实现这一目标的两种可能性:
第一个可能性
import UIKit
class ViewController: UIViewController {
@IBOutlet var myUIImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// You can also manually set it through IB
// selecting the UIImageView in the storyboard
// and checking "User Interaction Enabled" checkbox
// in the Attributes Inspector panel.
self.myUIImageView.userInteractionEnabled = true
}
// You can choose to use one of the UIResponder methods:
// touchesBegan, touchesMoved, touchesEnded etc, in order to detect the touch
// on the UIImageView.
override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
let touch: UITouch? = touches.first
if touch?.view == myUIImageView {
println("myUIImageView has been tapped by the user.")
}
super.touchesEnded(touches, withEvent: event)
}
}
Run Code Online (Sandbox Code Playgroud)
第二种可能性
import UIKit
class ViewController: UIViewController {
@IBOutlet var myUIImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
let singleTap = UITapGestureRecognizer(target: self, action: "myUIImageViewTapped:")
singleTap.numberOfTapsRequired = 1 // Initialized to 1 by default
singleTap.numberOfTouchesRequired = 1 // Initialized to 1 by default
self.myUIImageView.addGestureRecognizer(singleTap)
self.myUIImageView.userInteractionEnabled = true
}
func myUIImageViewTapped(recognizer: UITapGestureRecognizer) {
if(recognizer.state == UIGestureRecognizerState.Ended){
println("myUIImageView has been tapped by the user.")
}
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以将UIButton对象拖放到UIImageView.然后在故事板编辑器,只是使UIButton的Custom类型,渲染UIButton不可见的.然后click像处理任何按钮一样处理事件.
| 归档时间: |
|
| 查看次数: |
62721 次 |
| 最近记录: |