gpt4 book ai didi

objective-c - subview 上无法识别手势

转载 作者:行者123 更新时间:2023-12-01 18:27:27 25 4
gpt4 key购买 nike

我正在尝试识别 subview 中的手势。我只有在 super View 中定义手势识别器时才会得到响应。我想知道为什么我在 subview 中定义它时没有得到 gestutre 响应,在我的情况下是“View.m”,尽管当我在 super View 中定义手势处理程序时得到响应,在我的情况下是 View ViewController 但我想为什么它不能在 subview 中工作。该代码将使其更加清晰。此外,在动画期间手势无法识别,因为在我的情况下,我在“view.m”中将 subview 从左向右移动,并且当我在动画期间单击时手势无法识别。我试过 UIViewAnimationOptionAllowUserInteraction 但仍然没有识别手势。

这是带有 TapgesturHandler 的 SubView 类

//  View.m
// GestrureonImageView
//
// Created by Noman Khan on 8/24/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "View.h"

@implementation View

- (id)initWithFrame:(CGRect)frame
{

self = [super initWithFrame:frame];
if (self) {
// Initialization code
}

UITapGestureRecognizer *tapGestures=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesturesCallBack:)];

// NSLog(@"init gestures");

[tapGestures setDelegate:self];

// Set required taps and number of touches
[tapGestures setNumberOfTapsRequired:1];
[tapGestures setNumberOfTouchesRequired:1];
[self addGestureRecognizer:tapGestures];
return self;
}
-(void) gesturesCallBack:(UITapGestureRecognizer *)sender
{
NSLog(@"abcView");
[UIView animateWithDuration:10

delay:0.1
options:UIViewAnimationOptionAllowUserInteraction
animations:^
{
// [self initGestures];
CGAffineTransform transform = CGAffineTransformMakeTranslation(500,0);

self.transform = transform;
}
completion:^(BOOL finished){


}];

}



-(void)viewDidLoad{

NSLog(@"Inview");

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/

@end

和 View.h
//  View.h
// GestrureonImageView
//
// Created by Noman Khan on 8/24/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface View : UIView<UIGestureRecognizerDelegate>

@end

这是 ViewController.m 类
//  ViewController.m
// GestrureonImageView
//
// Created by Noman Khan on 8/24/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize v;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

-(void) gesturesCallBack:(UITapGestureRecognizer *)sender
{
NSLog(@"abc");

}

- (void)viewDidLoad
{
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor blueColor]];

UITapGestureRecognizer *tapGestures=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(gesturesCallBack:)];

// NSLog(@"init gestures");

[tapGestures setDelegate:self];

// Set required taps and number of touches
[tapGestures setNumberOfTapsRequired:1];
[tapGestures setNumberOfTouchesRequired:1];

[self.view addGestureRecognizer:tapGestures];
v=[[View alloc]initWithFrame:CGRectMake(40, 50, 80, 50)];
v.backgroundColor=[UIColor yellowColor];
// [v addGestureRecognizer:tapGestures];
[self.view addSubview:v];


// Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

这是我的 ViewController.h
//  ViewController.h
// GestrureonImageView
//
// Created by Noman Khan on 8/24/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "View.h"

@interface ViewController : UIViewController <UIGestureRecognizerDelegate>


@property(nonatomic,strong)View *v;

@end

最佳答案

试试设置v.userInteractionEnabled = YES

关于objective-c - subview 上无法识别手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12125247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com