- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
更新2
即使问题仍然存在,当前形式的问题也已得到回答和接受。该代码现在以0警告进行编译,但仍然对手势无响应。解决问题后,将希望通过准备新问题的过程来更新代码。一个新问题已发布here。
据我所知,在我所阅读的任何帖子中都从未问过或回答过这个问题(请参阅下面的重复内容)。
我正在尝试编写Objective C测试代码,以从iPhone屏幕上由矩形UIView
定义的任何区域中长按,点击或移动消息。到目前为止,如果UILongPressGesture,
和handler方法都位于UITapGesture
中,则我可以使用UIPanGesture
UIView
或UIViewController
可靠地发送离散或连续消息。
最终,我想将手势消息从多个子类的UIViews
发送到UIViewController
中的常见处理程序,但是在尝试执行此操作时遇到了问题。在编写代码时,我研究了有关使用UIGestureRecognition的委托以及Touchs中的Objective C方法(请参阅下面的注释)和SO帖子中的示例(下面的链接)一起使用的Apple文档。
该代码无法响应手势输入。最初,它编译时包含四个警告和两个问题:
OneOfSeveralSubviews.h
中,该协议现在称为
FirstGestureRecognizerDelegate.
。已在
ViewController
中进行了相关更改,例如
@interface ViewController : UIViewController <FirstSubviewGestureDelegate, SecondSubviewGestureDelegate>
id
(建议为
here)),对三个手势声明(即
initWithTarget:(id)self
)进行了类似的更改,并在每个
self
实例中添加了一个委托属性,从而将三个警告静音了,从而将消息发送到预期的目标-即
(id)self.delegate
而不是
(id)self
userInteractionEnabled
,
minimumPressDuration
,
numberOfTapsRequired
和
numberOfTouchesRequired
)检查了代码,并确保在
UIViewController.h.
中指定委托
duplicate
?
self
而是ViewController中的方法的委托目标?
.delegate
手势声明中删除了
handleTap:
属性,并且点击了红色方块,则程序将崩溃,并显示以下日志:
[OneOfSeveralSubviews handleTapSelect:] : unrecognized selector sent to instance 0x7fbe27d064b0
.delegate
或
handleLongPress:
手势声明中删除了
handlePan:
属性,则会出现类似的崩溃日志。
handleLongPress:
(2)
handleTapSelect:
和(3)
handlePan:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
OneOfSeveralSubviews *oneForAll=[[OneOfSeveralSubviews alloc] initView:[UIScreen mainScreen].bounds];
[self.view addSubview:oneForAll];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan)
{
NSLog(@"started");
self.timer=[NSTimer scheduledTimerWithTimeInterval:1.0
target:self
selector:@selector(stillPressing:)
userInfo:nil
repeats:YES];
} else if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
{
[self.timer invalidate];
self.timer = nil;
NSLog(@"ended");
}
}
- (void)stillPressing:(NSTimer *)timer
{
NSLog(@"pressing");
}
- (void)handleTapSelect:(id)sender
{
NSLog(@"tapped");
}
- (void)handlePan:(UIPanGestureRecognizer*)gestureRecognizer
{
[self adjustAnchorPointForGestureRecognizer:gestureRecognizer];
UIView *piece = [gestureRecognizer view];
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan || [gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGPoint translation = [gestureRecognizer translationInView:[piece superview]];
[piece setCenter:CGPointMake([piece center].x + translation.x, [piece center].y + translation.y)];
[gestureRecognizer setTranslation:CGPointZero inView:[piece superview]];
NSLog(@"%f %f", translation.x, translation.y);
[UIView animateWithDuration:0.75
delay:0
usingSpringWithDamping:0.75
initialSpringVelocity:0
options:UIViewAnimationOptionCurveLinear
animations:^{
piece.frame = touchFrame;
}
completion:^(BOOL finished) {
}];
}
}
- (void)adjustAnchorPointForGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan) {
UIView *piece = gestureRecognizer.view;
CGPoint locationInView = [gestureRecognizer locationInView:piece];
CGPoint locationInSuperview = [gestureRecognizer locationInView:piece.superview];
piece.layer.anchorPoint = CGPointMake(locationInView.x / piece.bounds.size.width, locationInView.y / piece.bounds.size.height);
piece.center = locationInSuperview;
}
}
#import <UIKit/UIKit.h>
#import "OneOfSeveralSubviews.h"
@interface ViewController : UIViewController <FirstSubviewGestureDelegate, SecondSubviewGestureDelegate>
{
CGRect touchFrame;
}
@property (strong, nonatomic) NSTimer *timer;
@end
#import "OneOfSeveralSubviews.h"
@implementation OneOfSeveralSubviews
- (id)initView:(CGRect)rect
{
self = [super initWithFrame:rect];
if (self)
{
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
CGPoint screenCentre = CGPointMake(width*0.5, height*0.5);
CGFloat fullSide = width * 0.33;
CGFloat halfSide = fullSide * 0.5;
touchFrame = CGRectMake(screenCentre.x - halfSide, screenCentre.y - halfSide, fullSide, fullSide);
UIView *hotspot = [[UIView alloc] initWithFrame:touchFrame];
hotspot.backgroundColor = [UIColor redColor];
UILongPressGestureRecognizer *longPressGR = [[UILongPressGestureRecognizer alloc] initWithTarget:(id)self.delegate action:@selector(handleLongPress:)];
[longPressGR setDelegate:(id)self.delegate];
[longPressGR setMinimumPressDuration:0.6f];
[longPressGR setNumberOfTapsRequired:1];
[longPressGR setNumberOfTouchesRequired:1];
[hotspot addGestureRecognizer:longPressGR];
UITapGestureRecognizer *tapGR = [[UITapGestureRecognizer alloc] initWithTarget:(id)self.delegate action:@selector(handleTapSelect:)];
[tapGR setDelegate:(id)self.delegate];
[tapGR setNumberOfTapsRequired:1];
[tapGR setNumberOfTouchesRequired:1];
[hotspot addGestureRecognizer:tapGR];
[self addSubview:hotspot];
UIPanGestureRecognizer *panGR = [[UIPanGestureRecognizer alloc] initWithTarget:(id)self.delegate action:@selector(handlePan:)];
[panGR setDelegate:(id)self.delegate];
[panGR setMinimumNumberOfTouches:1];
[panGR setMaximumNumberOfTouches:1];
[hotspot addGestureRecognizer:panGR];
self.userInteractionEnabled = YES;
}
return self;
}
@end
#import <UIKit/UIKit.h>
@protocol FirstSubviewGestureDelegate <NSObject>
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer;
- (void)handleTapSelect:(UITapGestureRecognizer*)gestureRecognizer;
- (void)handlePan:(UIPanGestureRecognizer*)gestureRecognizer;
@end
@interface OneOfSeveralSubviews : UIView
{
CGRect touchFrame;
}
- (id)initView:(CGRect)rect;
@property (assign) id<FirstSubviewGestureDelegate> delegate;
@end
最佳答案
UIGestureRecognizerDelegate
由UIKit声明。您不能创建自己的名为UIGestureRecognizerDelegate
的协议;这是一个相互矛盾的声明。
按照约定,带有UI前缀的类和协议由UIKit声明,并且您不应使用此前缀声明自己的对象和类。使用类似MyGestureRecognizerDelegate
的东西
关于ios - 如果忽略“UIGestureRecognizerDelegate”的重复协议(protocol)定义,如何定义目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49246176/
internal protocol Reducer { associatedtype S : BaseState associatedtype A : BaseActi
我在考虑我的应用程序中的验证检查,我认为在任何模型上调用 ValidatorFactory,实现 Validee,这意味着说哪个类负责 ValidatorCreation 听起来不错。但是下面的代码不
我已经定义了 2 个协议(protocol)。我需要第一个 (NameProtocol) 来执行 Equatable 协议(protocol)。而另一个类 (BuilderProtocol) 有一个返
在上传方面,WebDAV 协议(protocol)在哪些方面优于 HTTP 协议(protocol)。 Socket Upload 协议(protocol)和 WebDav Upload 协议(pro
是否可以在任何版本的 Swift 中扩展具有混合类/协议(protocol)类型约束的协议(protocol)?例如,仅当 Self 是 UIViewController 的子类并且符合 Protoc
我有一个协议(protocol) (ProtocolA),其中包含符合第二个协议(protocol) (ProtocolB) 的单个属性。 public protocol ProtocolA {
NSObject 协议(protocol)带有常用的协议(protocol)模板,但它似乎并不是协议(protocol)实际实现所必需的。将其排除在外似乎完全没有任何改变。那么,协议(protocol
我想根据这两种协议(protocol)的一般特征(例如开销(数据包)、安全性、信息建模和可靠性)来比较 OPC UA 和 MQTT。我在哪里可以找到每个协议(protocol)的开销和其他特性的一些示
使用 Swift 4,我正在尝试编写一个自定义协议(protocol),它提供对 @objc 协议(protocol)的一致性。 一些代码 更具体地说,我有一个自定义协议(protocol) Sear
我想定义一个在 Viper 架构中使用的协议(protocol),以使用具有弱属性的协议(protocol)在 Viper 组件之间建立连接,但我收到以下错误消息: 'weak' may only b
我在同一个网络中有 3 个 docker 容器: 存储 (golang) - 它提供了用于上传视频文件的 API。 主播 (nginx) - 它流式传输上传的文件 反向代理 (姑且称之为代理) 我有
我打算在我的项目中使用 php socket。它需要用户登录才能根据 session 填充内容。所以我的问题是,TCP/IP 协议(protocol)也像 HTTP 协议(protocol)一样为每个
目前,我的网站有两个版本。一种带有 https://-证书,一种没有。我想将我网站的 http 版本上的所有用户 301 重定向到我网站的 https://版本。 这似乎不可能,因为创建重定向将导致重
目前,我的网站有两个版本。一种带有 https://-证书,一种没有。我想将我网站的 http 版本上的所有用户 301 重定向到我网站的 https://版本。 这似乎不可能,因为创建重定向将导致重
我有一个 Swift View Controller ,它定义了一个在 Objective-C View Controller 中应该遵循的协议(protocol): ChildViewControl
我在客户那里有数百个硬件设备,需要通过telnet接口(interface)发送HTTP数据。 目标是等待数据的 Apache 2 Web 服务器和 PHP 脚本。 这已经可以正常工作了,但是我们发现
我发现如果我创建一个这样的协议(protocol): protocol MyProtocol { } 我不能这样做: weak var myVar: MyProtocol? 我找到了解决这个问题的方法
Xcode 基于模板生成了这个头文件: // this file is XYZAppDelegate.h #import @interface XYZAppDelegate : UIRespond
我在 github 中有一个公开的存储库,我正在开发一个开源应用程序,用于制作产品目录和小型 cms 内容。 我还有一个私有(private)仓库(不托管在github),它是在托管在github的开
您好,我想让别人看到私有(private) repo 代码,但不想公开我的 repo ,也不希望他们有能力更改内容。这可能吗?我查看了网站的“管理”部分,但没有找到合适的内容。谢谢大家。 最佳答案 据
我是一名优秀的程序员,十分优秀!