我在桌面视图上放置了一个`UIPanGestureRecognizer:
UIPanGestureRecognizer *swipe = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(closeCell:)];
swipe.minimumNumberOfTouches = 1;
swipe.maximumNumberOfTouches = 1;
swipe.delegate = self;
[self.tableView addGestureRecognizer:swipe];
我正在尝试将初始触摸y坐标设置为initialY
var。我尝试通过在swipe.state
等于UIGestureRecognizerStateBegan
时设置它来执行此操作。这样做的目的是在UIGestureRecognizerStateChanged
时使用它,但在此方法中,initalY
设置为1
。这是为什么?
-(void)closeCell:(UIPanGestureRecognizer *)swipe {
CGPoint pointInView = [swipe locationInView:swipe.view];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInView];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
int initalY;
int changeInMovement = 0;
if (indexPath == expandIndexPath) {
[self.tableView setScrollEnabled:NO];
if (swipe.state == UIGestureRecognizerStateBegan) {
CGPoint pointInCell = [swipe locationInView:cell];
initalY = pointInCell.y;
NSLog(@"BEGAN");
NSLog(@"INITIAL Y: %d", initalY);
}
if (swipe.state == UIGestureRecognizerStateChanged) {
CGPoint pointInCell = [swipe locationInView:cell];
int currentY = pointInCell.y;
changeInMovement = initalY - currentY;
NSLog(@"initial Y: %d", initalY);
NSLog(@"current Y: %d", currentY);
NSLog(@"change in Y: %d", changeInMovement);
//THE PROBLEM IS THAT INITAL Y BECOMES 1
}
}
}
答案 0 :(得分:1)
因为它是一个局部变量。它将被创建"每次调用该方法时都是新鲜的。
您可以将变量设为静态,这将保留其值。静态变量在实例(例如,具有相同类的所有viewControllers)之间以及方法调用之间共享。只需添加关键字static
:
static int initalY = 0;
static int changeInMovement = 0;