Ios 使用UIgestureRecognitor同时缩放、旋转和移动
我不明白为什么我的缩放、旋转和移动不能同时工作 我看了很多例子,但似乎找不到问题所在。 首先,我认为这是一个Ios 使用UIgestureRecognitor同时缩放、旋转和移动,ios,objective-c,uigesturerecognizer,Ios,Objective C,Uigesturerecognizer,我不明白为什么我的缩放、旋转和移动不能同时工作 我看了很多例子,但似乎找不到问题所在。 首先,我认为这是一个手势识别器应该与手势识别器同时识别的问题,但这不是请帮助:) 这是我的密码: @implementation StoryEditorPageHolderView - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecog
手势识别器应该与手势识别器同时识别的问题,但这不是请帮助:)
这是我的密码:
@implementation StoryEditorPageHolderView
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer{
return YES;
}
- (id)initWithProperty:(Property *)property scale:(CGFloat)scaleFactor pos:(CGPoint)point dustbin:(DustbinView *)dustBin{
self = [super initWithFrame:CGRectZero];
if(self){
self.imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:property.imageName]];
self.frame = CGRectMake(0, 0, self.imageView.frame.size.width, self.imageView.frame.size.height);
[self addSubview:_imageView];
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[self addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[self addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapProfileImageRecognizer setNumberOfTapsRequired:2];
[self addGestureRecognizer:tapProfileImageRecognizer];
}
return self;
}
- (void)rotate:(UIRotationGestureRecognizer *)rotate {
if (rotate.state == UIGestureRecognizerStateBegan) {
prevRotation = 0.0;
}
float thisRotate = rotate.rotation - prevRotation;
prevRotation = rotate.rotation;
self.transform = CGAffineTransformRotate(self.transform, thisRotate);
}
- (void)scale:(UIPinchGestureRecognizer *)pinch {
if (pinch.state == UIGestureRecognizerStateBegan)
prevPinchScale = 1.0;
float thisScale = 1 + (pinch.scale-prevPinchScale);
prevPinchScale = pinch.scale;
self.transform = CGAffineTransformScale(self.transform, thisScale, thisScale);
}
-(void)move:(UIPanGestureRecognizer *)pan {
if (pan.state == UIGestureRecognizerStateBegan){
prevPanPoint = [pan locationInView:self.superview];
}
CGPoint curr = [pan locationInView:self.superview];
float diffx = curr.x - prevPanPoint.x;
float diffy = curr.y - prevPanPoint.y;
CGPoint centre = self.center;
centre.x += diffx;
centre.y += diffy;
self.center = centre;
prevPanPoint = curr;
}
@end
在.h文件中,我还将uigesturecognifizerdelegate
作为delegate
:
#import <Foundation/Foundation.h>
@class Property;
@class DustbinView;
@interface StoryEditorPropertyView : UIView <UIGestureRecognizerDelegate>
@property (strong, nonatomic) UIPanGestureRecognizer *panRecognizer;
@property (strong, nonatomic) UIPinchGestureRecognizer *pinchRecognizer;
@property (strong, nonatomic) UIRotationGestureRecognizer *rotationRecognizer;
@property (strong, nonatomic) UITapGestureRecognizer *tapProfileImageRecognizer;
@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) Property *property;
@property (nonatomic) CGPoint pointBegin;
@property (nonatomic) bool isRemoveable;
@property (nonatomic) CGFloat beginScale;
@property (strong, nonatomic) DustbinView *dustBin;
- (id)initWithProperty:(Property *)property scale:(CGFloat)scaleFactor pos:(CGPoint)point dustbin:(DustbinView *)dustBin;
@end
#导入
@阶级属性;
@类视图;
@接口StoryEditorPropertyView:UIView
@属性(强,非原子)UIPangestureRecognitor*装甲识别器;
@属性(强,非原子)UIPinchEstureRecognitor*PinchRecognitor;
@属性(强,非原子)UIRotationGestureRecognitor*RotationRecognitor;
@属性(强,非原子)UITapGestureRecognitor*TapproFileImageRecognitor;
@属性(强,非原子)UIImageView*imageView;
@性质(强,非原子)性质*性质;
@属性(非原子)CGPoint pointBegin;
@属性(非原子)布尔是可移除的;
@属性(非原子)CGFloat beginScale;
@属性(强、非原子)垃圾箱视图*垃圾箱;
-(id)initWithProperty:(Property*)属性比例:(CGFloat)比例因子位置:(CGPoint)点垃圾箱:(DustbinView*)垃圾箱;
@结束
好的,错误之处在于没有添加UIGestureRecognitor要适应的对象。例如,您有
UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapProfileImageRecognizer setNumberOfTapsRequired:2];
[self addGestureRecognizer:tapProfileImageRecognizer];
除了这条线,这正是你需要的
[self addGestureRecognizer:tapProfileImageRecognizer];
这是将手势添加到类中。您需要一个对象:)
所以就这么做吧
[self.view addGestureRecognizer:tapProfileImageRecognizer];
对于你所有的手势,它应该起作用:)
您必须始终确保添加到对象上,而不是类上
希望这有帮助
编辑:
将UIGestureRecognizers放在UIView上是您正在做的事情的方式。你现在需要做的是进入Xcode
- 文件->新文件
- 创建UIViewController子类
- 一定要把它命名为
- 在.xib文件下,找到以下内容:
然后,如果查看UIView的位置,请单击该位置。这将打开UIView属性面板。移动到圆圈中有i的小按钮。它是属性面板下的蓝色。单击该按钮,您将看到以下内容:
在这里,您可以看到标有“UIView”的框
这就是您键入UIView子类的地方
这将有助于解决您遇到的任何问题。希望有帮助!:) 添加手势代码您应该将手势代码放入viewDidLoad
而不是init
方法中,并将手势添加到视图(self.view
)而不是对象(self
)本身中。
我做了一些改变
-(void) viewDidLoad
{
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[self.view addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[self.view addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self.view addGestureRecognizer:panRecognizer];
UITapGestureRecognizer *tapProfileImageRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapProfileImageRecognizer setNumberOfTapsRequired:2];
[self.view addGestureRecognizer:tapProfileImageRecognizer];
}
这对我很有用:
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
pinchRecognizer.delegate = self;
[self addGestureRecognizer:pinchRecognizer];
UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
pinchRecognizer.delegate = self;
[self addGestureRecognizer:rotationRecognizer];
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
pinchRecognizer.delegate = self;
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[self addGestureRecognizer:panRecognizer];
它告诉我:在类型为“xxxxxxx”的对象上找不到属性“view”。您的类a的子类是什么?您是在使用UIViewController还是任何其他控制器?或者类似UIView的子类?哦!您需要做的是将其保留为self,因为您已经将其添加到视图中了。现在,您所要做的就是创建一个UIViewController类,在.xib文件中,转到它的视图的规范,并键入您的UIView子类,我将在一个新的答案中发布它挂起!!很抱歉,我没有首先告诉您:)这是真的,我没有在那里看到init方法!hahaBut这是加载视图后将在用户选项卡上生成的属性吗?我不能使用viewDidLoad方法,可以吗?他们在添加手势时没有使用UIViewController。它是UIView@MadsDonkin请参阅查看组件完全加载且所有IB连接完成时调用viewDidLoad。在viewDidLoad之前,我们不应该使用view及其组件。所以,如果它不起作用,请再试一次。请告诉我更多关于这个类的信息,它是做什么的,它是控制器还是视图???