在UIImageView我的子类初始化后,我有以下代码行:
self.userInteractionEnabled = true
self.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "handleTap:"))
Run Code Online (Sandbox Code Playgroud)
我创建了必要的相关功能:
func handleTap(gestureRecognizer: UITapGestureRecognizer) {
print("In handler")
}
Run Code Online (Sandbox Code Playgroud)
在点击有问题的视图时,"处理程序从未打印到控制台".然后我删除了处理函数,看看编译器是否会抱怨丢失的函数.它没有.
我很难过.我真的很感激任何光明人都可以放弃这一点.
更新:我的课实际上是一个UIImageView相反的UIView
我在主视图中定义了多个视图.我想为所有这些视图添加单击手势.下面是我编写的代码,但这会将轻击手势注册到我添加的最后一个视图.因此,在下面的代码中,仅messagesView针对其他视图注册而不是其他视图.我有两个问题:
如何在多个视图中注册相同的tapGesture?
让我们假设我得到了这个工作,现在从这些视图中的所有单击都转到相同的函数调用oneTap.在此功能中,我如何区分水龙头来自哪个视图?
码:
@synthesize feedsView, peopleView, messagesView, photosView;
- (void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(oneTap:)];
[singleTap setNumberOfTapsRequired:1];
[singleTap setNumberOfTouchesRequired:1];
[feedsView addGestureRecognizer:singleTap];
[peopleView addGestureRecognizer:singleTap];
[messagesView addGestureRecognizer:singleTap];
//[photosView addGestureRecognizer:singleTap];
[singleTap release];
return;
}
Run Code Online (Sandbox Code Playgroud) 因为我无法使用任何框架来创建相册,所以我正在尝试使用Collection View创建自己的框架,但是我在开始时就卡住了.
我的目标是将我的Web服务中的所有图像显示到我的集合视图中,因为所有图像显示,下一步是当有人点击任何单元格时,我可以在新视图中打开它并在所有图像之间导航.
这是我创建的基本代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[collectionController reloadData];
tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:nil action:@selector(touched)];
tapGesture.numberOfTapsRequired = 1;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 6;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *cellIdentifier = @"Cell";
CollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
[cell.imgCollection setImageWithURL:[NSURL URLWithString:@"http://sallescds.com.br/wp-content/uploads/2012/12/xepop-300x300.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
[cell.imgCollection addGestureRecognizer:tapGesture];
return …Run Code Online (Sandbox Code Playgroud) 我有两个UI按钮,我想在两者上实现Longpressgesture.
所以我写了下面的代码..
-(void)viewdidLoad
{
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(buttonLongPressed:)];
longPress.minimumPressDuration = 0.5;
[Button1 addGestureRecognizer:longPress];
[Button2 addGestureRecognizer:longPress];
}
- (void)buttonLongPressed:(UILongPressGestureRecognizer *)sender
{
if(sender.state == UIGestureRecognizerStateBegan)
{
}
}
Run Code Online (Sandbox Code Playgroud)
现在我的疑问是如何检查哪个按钮是长按?
谢谢Ranjit
我知道已经有很多问题与此类似,但我已经尝试了所有这些问题,而且由于未能解决我的问题,我发布了我的问题.首先我尝试的问题是:
1)如何通过longPressGestureRecognizer获取button.tag?
在我的应用程序UIButtons中,我的xib中有12个.长期以来,UIButton我有这种方法被调用.gesture.view.tag每次点击不同时,使用属性总是给我相同的标记(即)UIButtons.
- (IBAction)longPress:(id)sender {
UILongPressGestureRecognizer* gesture=(UILongPressGestureRecognizer*)sender;
NSLog(@"Tag---> %d",gesture.view.tag);
}
Run Code Online (Sandbox Code Playgroud)
我的xib看起来像这样:

更新1:
在有人与xib混淆之前,我必须说它UIButtons被设置为自定义类型,因此它们是不可见的UIImageView.
我在我的视图中设置了一个UILongPressGestureRecognizer来处理四个不同的按钮,如何在我的代码中访问哪个按钮被点击?
我的UILongPressGestureRecognizer看起来像这样:
@IBAction func editText(sender: UILongPressGestureRecognizer) {
textFieldInput.hidden = false
iphoneSaveCharName.hidden = false
}
Run Code Online (Sandbox Code Playgroud)
我想使用长按,以便我可以编辑按钮文本
编辑1:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var iphoneTableView: UITableView!
@IBOutlet weak var textFieldInput: UITextField!
@IBOutlet weak var iphoneSaveCharName: UIButton!
@IBOutlet weak var charOne: UILabel!
@IBOutlet weak var charTwo: UILabel!
@IBOutlet weak var charTree: UILabel!
@IBOutlet weak var charFour: UILabel!
@IBOutlet weak var test1: UIButton! //button that I am clicking on!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading …Run Code Online (Sandbox Code Playgroud) 我正在研究 Pan Gesture 的样本。我很想知道我可以在两个视图上添加单个手势吗?
我的代码如下:
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(invokePanGesture:)];
[panGesture setMinimumNumberOfTouches:1];
[panGesture setMaximumNumberOfTouches:2];
[btnGreen addGestureRecognizer:panGesture];
[btnYellow addGestureRecognizer:panGesture];
Run Code Online (Sandbox Code Playgroud)
我的处理程序方法如下:
- (void)invokePanGesture:(UIPanGestureRecognizer*)recognizer {
CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}
Run Code Online (Sandbox Code Playgroud)
现在的问题是手势识别器在我的情况下只考虑最后一个视图btnYellow作为它的视图。如果我创建两个单独的 GestureRecognizer 对象,它就可以工作。所以请让我明白:
是否可以在多个视图中使用单个手势?
如果是,那么如何?
如果是现在,为什么?
提前致谢
所以我有以下代码:
UITapGestureRecognizer *showNewsStoryTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showNewsStory:)];
[self.storyImageView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyTitleLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyImageFailedLabel_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[self.storyImageFailedTextView_ addGestureRecognizer:showNewsStoryTapGestureRecognizer];
[showNewsStoryTapGestureRecognizer release];
Run Code Online (Sandbox Code Playgroud)
似乎这只适用于一个UIView,这是最后添加的一个.换句话说UITapGestureRecognizer,它的视图是一对一的关系.它是否正确?我该如何解决?我是否必须UITapGestureRecog为每个人创建一个单独的?
多个UIImageView上的TapGesture识别器无法正常工作,同时它检测到最后添加的图像视图手势..我已经这样做了,
UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myFunction:)];
tapped.numberOfTapsRequired = 1;
tapped.delegate = self;
UIImageView *sample_book1= [[UIImageView alloc]initWithFrame:CGRectMake(70, 135, 100,125) ];
sample_book1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"mathematics.png"]];
sample_book1.userInteractionEnabled = YES;
sample_book1.tag = 0;
[sample_book1 addGestureRecognizer:tapped];
[self.view addSubview:sample_book1];
UIImageView *sample_book2= [[UIImageView alloc]initWithFrame:CGRectMake(220, 135, 100,125) ];
sample_book2.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"biology.png"]];
sample_book2.userInteractionEnabled = YES;
sample_book2.tag = 1;
[sample_book2 addGestureRecognizer:tapped];
[self.view addSubview:sample_book2];
UIImageView *sample_book3= [[UIImageView alloc]initWithFrame:CGRectMake(370, 135, 100,125) ];
sample_book3.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"chemistry.png"]];
sample_book3.userInteractionEnabled = YES;
sample_book3.tag = 2;
[sample_book3 addGestureRecognizer:tapped];
[self.view addSubview:sample_book3]; …Run Code Online (Sandbox Code Playgroud) 我希望能够将这些线条切断:
[button0 addGestureRecognizer:longPress];
[button1 addGestureRecognizer:longPress];
[button2 addGestureRecognizer:longPress];
[button3 addGestureRecognizer:longPress];
[button4 addGestureRecognizer:longPress];
[button5 addGestureRecognizer:longPress];
[button6 addGestureRecognizer:longPress];
[button7 addGestureRecognizer:longPress];
[button8 addGestureRecognizer:longPress];
[button9 addGestureRecognizer:longPress];
Run Code Online (Sandbox Code Playgroud)
等等.一路到36 !!
可能有一个循环?但我不知道该怎么做.
感谢和问候.
我在滚动视图中创建动态标签,我想为所有这些动态生成的标签添加单个手势识别器.我正在创建如下手势
UIPanGestureRecognizer *gesture = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)] ;
Run Code Online (Sandbox Code Playgroud)
现在我想将此手势添加到多个标签.是否可以为动态创建的标签添加相同的手势?
我有 2 个 UIImageView 和一个 tapGestureRecognizer。
override func viewDidLoad() {
// Do any additional setup after loading the view.
super.viewDidLoad()
let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(cameraTapped(tapGestureRecognizer:)))
cameraUIImageView.isUserInteractionEnabled = true
cameraUIImageView.addGestureRecognizer(tapGestureRecognizer)
plus1UIImageView.isUserInteractionEnabled = true
plus1UIImageView.addGestureRecognizer(tapGestureRecognizer)
//
}
Run Code Online (Sandbox Code Playgroud)
我只能点击第二个 UIImageView,它是 plus1UIImageView。
为什么?
我需要将 a 添加UITapGestureRecognizer到多个,UILabel并且它们都需要转到相同的函数进行处理。
我有的是这个:
@IBOutlet weak var label_something: UILabel!
let tap = UITapGestureRecognizer(target: self, action: #selector(self.myFunction))
label_something.addGestureRecognizer(tap)
Run Code Online (Sandbox Code Playgroud)
在这里收到:
@objc func myFunction(sender:UITapGestureRecognizer) { // Something... }
Run Code Online (Sandbox Code Playgroud)
像魅力一样工作。问题是它只适用于一个 UILabel(如果添加addGestureRecognizer(tap)到多个UIlabel它只适用于添加的最后一个)
所以我的问题是:
如何实现我想在这里做的事情?带有 tapRecognizer 的五个不同的 UILabels 具有相同的功能
ios ×10
objective-c ×6
iphone ×5
swift ×4
xcode ×2
ios4 ×1
ipad ×1
sdk ×1
storyboard ×1
uibutton ×1
uiimageview ×1
uilabel ×1
uiview ×1