作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我目前正在使用 touchesMoved
允许用户向上滑动,向上滑动的次数越多,增加的次数就越大。我不认为我可以在这里使用 UIGestureRecognizer
,因为它不会记录每一个 Action - 它只会记录单次滑动。
这是我当前的代码:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint gestureEndPoint = [touch locationInView:self.view];
NSLog(@"START: %@ END: %@", NSStringFromCGPoint(gestureStartPoint), NSStringFromCGPoint(gestureEndPoint));
if((gestureStartPoint.x - gestureEndPoint.x) < 20 && (gestureStartPoint.x - gestureEndPoint.x) > -20)
{
if((gestureStartPoint.y - gestureEndPoint.y) > (gestureStartPoint.x - gestureEndPoint.x))
{
if(gestureStartPoint.y > gestureEndPoint.y)
{
NSInteger integer = (NSInteger)(gestureEndPoint.y - gestureStartPoint.y);
NSString *string = [NSString stringWithFormat:@"%i", integer / 40];
if([string intValue] && self.lastValue != [string intValue])
{
[self.timer invalidate];
[self.updateCounter invalidate];
[self.secondsTimer invalidate];
self.lastValue = [string integerValue];
self.number = self.number + 1;
NSString *finalTime = [NSString stringWithFormat:@"%i", self.number];
if(self.number == 1)
finalTime = [finalTime stringByAppendingString:@" MINUTE"];
else
finalTime = [finalTime stringByAppendingString:@" MINUTES"];
[self.amountOfTime setText:finalTime];
lastPoint = gestureEndPoint;
NSLog(@"Setting timer for %i", self.number * 60);
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.number * 60 target:self selector:@selector(countdownEnded) userInfo:nil repeats:NO];
self.updateCounter = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(updateCounterText) userInfo:nil repeats:YES];
CABasicAnimation *crossFade = [CABasicAnimation animationWithKeyPath:@"contents"];
crossFade.duration = 2.0;
crossFade.fromValue = (__bridge id)([self.background.image CGImage]);
crossFade.toValue = (__bridge id)([[UIImage imageNamed:@"MainBackground"] CGImage]);
crossFade.removedOnCompletion = NO;
crossFade.fillMode = kCAFillModeForwards;
[self.background.layer addAnimation:crossFade forKey:@"animateContents"];
}
}
else
{
if(self.number - 1 >= 0)
{
[self.timer invalidate];
[self.updateCounter invalidate];
[self.secondsTimer invalidate];
self.number = self.number - 1;
NSString *finalTime = [NSString stringWithFormat:@"%i", self.number];
if(self.number == 1)
finalTime = [finalTime stringByAppendingString:@" MINUTE"];
else
finalTime = [finalTime stringByAppendingString:@" MINUTES"];
[self.amountOfTime setText:finalTime];
lastPoint = gestureEndPoint;
NSLog(@"Setting timer for %i", self.number * 60);
self.timer = [NSTimer scheduledTimerWithTimeInterval:self.number * 60 target:self selector:@selector(countdownEnded) userInfo:nil repeats:NO];
self.updateCounter = [NSTimer scheduledTimerWithTimeInterval:60 target:self selector:@selector(updateCounterText) userInfo:nil repeats:YES];
}
else
self.number = 0;
}
}
}
}
问题是 - 如果您从左向右或从右向左或从上向下滑动,数字会增加。我只希望 if 语句中的位选择从下到上的滑动。我怎样才能做到这一点并消除所有其他滑动?
最佳答案
像这样?
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint gestureEndPoint = [touch locationInView:self.view];
int dx = abs(gestureStartPoint.x - gestureEndPoint.x);
int dy = -1 * (gestureEndPoint.y - gestureStartPoint.y);
if(dx > 20) {
// too much left/right, so don't do anything
return;
}
if(dy < 0) {
// they moved down
return;
}
NSLog(@"Change in Y: %d", dy); // now do something with dy
// ....
关于ios - 如何仅在 touchesMoved 上查找向上滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14661894/
我是一名优秀的程序员,十分优秀!