我已经通过这样的代码创建了图像视图 -
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:@"draw.png"];
[self.view addSubview:dot];
}
我想向此UIImageView
添加用户互动,然后在点击时为此UIImageView
创建一个选择器或操作,该如何完成?
答案 0 :(得分:0)
试试这样: -
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:@"draw.png"];
[self.view addSubview:dot];
1)要实现所需的触摸次数为1,请参阅: -
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewDoubleTapped:)];
doubleTapRecognizer.numberOfTapsRequired = 2;
doubleTapRecognizer.numberOfTouchesRequired = 1;
[dot addGestureRecognizer:doubleTapRecognizer];
- (void)doubleTapped:(UITapGestureRecognizer*)recognizer
{
}
2)要实现所需的触摸次数为2,请参阅: -
UITapGestureRecognizer *twoFingerTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTwoFingerTapped:)];
twoFingerTapRecognizer.numberOfTapsRequired = 1;
twoFingerTapRecognizer.numberOfTouchesRequired = 2;
[dot addGestureRecognizer:twoFingerTapRecognizer];
- (void)twoFingerTapped:(UITapGestureRecognizer*)recognizer
{
}
答案 1 :(得分:0)
这取决于你想要达到的行动类型。一般来说,您使用的是UIGestureRecognizer。例如,如果您希望图像响应轻击手势,那么您将拥有以下内容 - (void)viewDidLoad { [super viewDidLoad]; //在加载视图后进行任何其他设置,通常是从笔尖 dotArray = [NSMutableArray alloc] init]; UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapGesture:)];
for(int i = 0; i< 30; i++) {
UIImageView *dot =[[UIImageView alloc] ...
dot.image=[UIImage imageNamed:@"draw.png"];
dot.tag = i; //identify dot image.
[self.view addSubview:dot];
[dotArray addObject:dot];
[dot addGestureRecognizer:tapGesture];
...
}
[tapGesture release];
}
然后处理轻击手势的方法......
-(void)handleTapGesture:(id)sender {
UITapGestureRecognizer * tapGesture = (UITapGestureRecognizer*)sender;
for(int i = 0; i<[dotArray count]; i++) {
UIImageView * dot = (UIImageView*)[dotArray objectAtIndex:i];
if(dot.tag == [tapGesture view].tag) {
//fade out animation
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5f];
dot.alpha = 0.0f;
[UIView commitAnimations];
}
要使其工作,您需要创建一个点数组并将其声明为实例变量,否则该方法无法访问该点。
答案 2 :(得分:0)
谢谢大家的帮助,我把每个人的答案放在一起,并把它作为我的最终代码。
//
// ViewController.m
// InvaderRush
//
// Created by Ajay Venkat on 13/12/2014.
// Copyright (c) 2014 AJTech. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
NSArray *dotArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,100,100)];
dot.image=[UIImage imageNamed:@"invader.jpg"];
[self.view addSubview:dot];
dot.tag = 1; //identify dot image.
UITapGestureRecognizer *doubleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doubleTapped:)];
doubleTapRecognizer.numberOfTouchesRequired = 1;
[dot addGestureRecognizer:doubleTapRecognizer];
dot.userInteractionEnabled = YES;
NSMutableArray *images =[[NSMutableArray alloc] initWithObjects: dot,nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)doubleTapped:(id)sender {
UIGestureRecognizer *recognizer = (UIGestureRecognizer*)sender;
UIImageView *imageView = (UIImageView *)recognizer.view;
if(imageView.tag==1) {
[imageView setImage:[UIImage imageNamed:@"space_invader.jpg"]];
}
}
-(void)handleTapGesture:(id)sender {
}
@end