gpt4 book ai didi

ios - UIImageView 的深层复制没有复制 iOS 中应用于原始 View 的所有委托(delegate)和手势

转载 作者:行者123 更新时间:2023-11-29 03:24:21 25 4
gpt4 key购买 nike

这是我如何复制对象的复制代码

 - (id)copyWithZone:(NSZone *)zone {

dragable *obj =[[[self class]allocWithZone:zone ] init];
if (obj) {
obj->startLocation=startLocation;
obj->_firstX=_firstX;
obj->_firstY=_firstY;
obj->_lastRotation=_lastRotation;
obj->_lastScale=_lastScale;
obj.center=self.center;
obj.frame=self.frame;
obj.bounds=self.bounds;
obj.backgroundColor = self.backgroundColor;
obj.image=self.image;
}
return obj;
}

dragable 类是自定义的 uiimageView 类,上面应用了一些委托(delegate)和手势,这里有一些关于可拖动类的内部细节

@protocol draggableDelegate <NSObject>

@optional
-(void) currentObjectClicked:(UIImageView* )object;

@end

@interface dragable : UIImageView<NSCopying,UIGestureRecognizerDelegate>
{
CGPoint startLocation;
CGFloat _lastScale;
CGFloat _lastRotation;
CGFloat _firstX;
CGFloat _firstY;
}
@property (nonatomic, assign) id<draggableDelegate> delegate;
@property (atomic,retain ) CAShapeLayer *marque;
@property (atomic,assign ) CGPoint startLocation;
@property (atomic,assign ) CGFloat _lastScale;
@property (atomic,assign ) CGFloat _lastRotation;
@property (atomic,assign ) CGFloat _firstX;
@property (atomic,assign ) CGFloat _firstY;



@end

所应用的手势都在同一个类中

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)] ;
[pinchRecognizer setDelegate:self];
[self addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)] ;
[rotationRecognizer setDelegate:self];
[self addGestureRecognizer:rotationRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)] ;
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[self addGestureRecognizer:panRecognizer];

UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)] ;
[tapProfileImageRecognizer setNumberOfTapsRequired:1];
[tapProfileImageRecognizer setDelegate:self];
[self addGestureRecognizer:tapProfileImageRecognizer];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
[self addGestureRecognizer:longPress];

所以重点是我正在处理可拖动对象,如上面(最上面)代码所示,它与该对象一起工作现在我想复制应用于可拖动对象的所有 GestureRecognizer 和委托(delegate),所以当我使用副本时我应该不将所有 GestureRecognizer 和委托(delegate)单独应用于其副本

最佳答案

如果你的手势识别器对于你的类总是相同的,将它们设置在你的初始化器中,这样当你调用 init 时它们会在你的副本中重新创建。在你的copyWithZone:方法(无论如何都必须重新创建它们,因为一个手势识别器实例不能与多个 View 相关联)。

将您的委托(delegate)复制到您的 copyWithZone: 中方法与复制其他实例变量 ( obj->delegate = _delegate; ) 相同。

另请注意,UIView 的指定初始化程序是 initWithFrame:initWithCoder:而不是 init , 而且不需要设置 center , bounds , 和 frame同时因为它们相互修饰。只需使用 initWithFrame:而不是 init (除非您移动了绑定(bind)矩形的原点)。

dragable *obj = [[[self class] allocWithZone:zone] initWithFrame:self.frame];

关于ios - UIImageView 的深层复制没有复制 iOS 中应用于原始 View 的所有委托(delegate)和手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20654962/

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