0
  - (void)viewDidLoad
 {
  [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
   container = [[UIView alloc] initWithFrame:self.view.frame];
UIImageView* main_im0 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon1.png"]];
[container addSubview:main_im0];
[self.view addSubview:container];

 }

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *tt = [touches anyObject];

UIImageView *touchedview=(UIImageView*)[tt view];


NSArray *views = [container subviews];



 for (UIImageView* im in views)
 {
    UIImageView* focus=im;
    if (touchedview==focus)
    {

    }

}

}

main_im0と呼ばれるImageViewを設定するこのコードがあります。これはUIViewコンテナーに入れられ、次にビューに入れられます。main_im0をクリックすると、touch関数がtouchedview==focusの条件に達すると思います。しかし、私はその条件をアクティブにすることができませんでしたか?どうしたの?

4

2 に答える 2

4

有効にしてください

main_im0.userInteractionEnabled = YES;

デフォルトでNoUIImageView

于 2012-08-16T06:40:01.113 に答える
1

ジェイソン、私はあなたのコードを誤解しているかもしれませんが、UIViewでタップジェスチャを実現する方法についてラウンドアバウトしていませんか?

次を使用できます。

// enable user interaction with this view
main_img0.userInteractionEnabled = YES;

// setup a tap gesture to call a method once triggered (like a javascript mouseClick event)
UITapGesture *tapGesture = [[UITapGesture alloc] initWithTarget:self selector:@selector(myMethodToDoSomething)];

[main_img0 addGestureRecognizer:tapGesture];

// if you are not using Automatic Reference Counting, then remember to release your allocated tapGesture object
[tapGesture release];


...somewhere later in your code....


// method to do something
-(void)myMethodToDoSomething
{
    NSLog(@"This method executed");
}

これで、main_img0ビューをタップすると、メソッド「myMethodToDoSomething」が実行されます。

ちなみに、フォトショップでデザインした独自の画像を使用したカスタムのボタンが必要な場合は、コードを使用してUIButtonを作成し、UIButtonの背景画像プロパティを設定するだけです。そのようです:

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];
[button setImage:[UIImage imageNamed:@"mybutton.png"] forControlState:UIControlStateNormal];
于 2012-08-16T07:13:25.940 に答える