我有一个ViewController iDragHomeViewController
和另一个
NSObject
班级iDrag
“iDragHomeViewController.m”
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *dragView = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 200, 200)];
[dragView setBackgroundColor:[UIColor greenColor]];
[self.view addSubview:dragView];
iDrag *drag = [[iDrag alloc]init];
[drag makeDraggableView:dragView];
}
“iDrag.m”
-(void)makeDraggableView: (UIView *)dragView {
UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(cellPan:)];
[dragView addGestureRecognizer:panRecognizer];
}
- (void)cellPan:(UIPanGestureRecognizer *)iRecognizer {
UIView *viewToDrag = [[UIView alloc]init];
viewToDrag = iRecognizer.view;
CGPoint translation = [iRecognizer translationInView:[viewToDrag superview]];
viewToDrag.center = CGPointMake(iRecognizer.view.center.x + translation.x,
iRecognizer.view.center.y + translation.y);
[iRecognizer setTranslation:CGPointMake(0, 0) inView:[viewToDrag superview]];
}
现在我在这里尝试的是通过应用 PanGesture使这个“ dragView
”(属于iDragHomeViewController
)在课堂上可拖动。iDrag
但是代码崩溃了。
我知道有些人会建议我使用NSNotification
在另一个类中处理 Pan 动作,但我不想写一行并只在 Class中iDragHomeViewController
处理所有内容。iDrag
可能吗 ??
请帮忙。