gpt4 book ai didi

ios - Multiview App 中的滑动手势

转载 作者:行者123 更新时间:2023-11-28 17:44:48 25 4
gpt4 key购买 nike

我是 iOS 开发的新手,正在努力开发 App。我无法在多 View 应用程序中使用滑动手势。代码贴出来了,有错误的请大侠指点。这是一个基于 View 的应用程序。

这里是mainViewController.h

 #import <UIKit/UIKit.h>

@interface mainViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
NSArray *listData;
}
@property (nonatomic, retain) NSArray *listData;
@end

现在mainViewController.m

#import "mainViewController.h"

@implementation mainViewController
@synthesize listData;


- (void)viewDidLoad {
NSArray *array =[[NSArray alloc] initWithObjects:@"Apple",@"Boy",@"Cat", nil];
self.listData = array;
[array release];
[super viewDidLoad];
}


- (void)dealloc
{
[listData release];
[super dealloc];
}


#pragma mark - View lifecycle

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
return [self.listData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *SimpleTableIdentifier = @"SimpleTableIdentifier";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:
SimpleTableIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:SimpleTableIdentifier] autorelease];
}

NSUInteger row = [indexPath row];
cell.textLabel.text = [listData objectAtIndex:row];
//cell.textLabel.font = [UIFont boldSystemFontOfSize:50];
return cell;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
if([rowValue isEqualToString:@"Apple"])
{
cell.backgroundColor = [UIColor redColor];
}
else
if([rowValue isEqualToString:@"Boy"])
cell.backgroundColor= [UIColor yellowColor];



}

- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSUInteger row = [indexPath row];
NSString *rowValue = [listData objectAtIndex:row];
if([rowValue isEqualToString:@"Apple"])
{
mainViewController* flashView=[[mainViewController alloc] initWithNibName:@"fl" bundle:[NSBundle mainBundle]];
[self.view addSubview:flashView.view];


}

//[tableView deselectRowAtIndexPath:indexPath animated:YES];
}


- (void)viewDidUnload
{
[super viewDidUnload];
self.listData=nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 110;
}


@end

我添加了一个 UIViewSubController 类,其中包含名为“fl”的 xib 文件。

在 fl.h 中:

#define kMinimumLength 5
#define kMaxVariance 1
#import <UIKit/UIKit.h>
#import "mainViewController.h"

@protocol flDelegate;

@interface fl : UIViewController <UIGestureRecognizerDelegate>
{

CGPoint gestureStartPoint;
id <flDelegate> delegate;

}
@property CGPoint gestureStartPoint;
@property (nonatomic,retain) id <flDelegate> delegate;
@end

@protocol flDelegate

-(IBAction)flDidFinish:(fl *)controller;

@end

现在在 fl.m 中:

#import "fl.h"

@implementation fl

@synthesize delegate;
@synthesize gestureStartPoint;


- (void)dealloc
{
[super dealloc];

}

#pragma mark - View lifecycle

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch =[touches anyObject];
gestureStartPoint =[touch locationInView:self.view];
}

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch =[touches anyObject];
CGPoint CurrentPosition =[touch locationInView:self.view];
if(CurrentPosition.x >= kMinimumLength)
{
NSLog(@"Go");
}
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

这里一切正常,但它没有检测到滑动,也没有打印 Go。

最佳答案

我还建议看一下 UISwipeGestureRecognizer,但如果您更喜欢保留当前的设计,这是我在其他地方使用的代码来检测滑动和捏合(缩放)。您需要根据您的情况调整它(您有三种方法而不是三个分支)。

        if (touch.phase == UITouchPhaseBegan) {
// NSLog(@"TOUCH BEGAN");
_initialView = touchView;
startTouchPosition1 = [touch locationInView:self];
startTouchTime = touch.timestamp;

if ([allTouches count] > 1) {
startTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self];
previousTouchPosition1 = startTouchPosition1;
previousTouchPosition2 = startTouchPosition2;
}
}

if (touch.phase == UITouchPhaseMoved) {
// NSLog(@"TOUCH MOVED");
if ([allTouches count] > 1) {
CGPoint currentTouchPosition1 = [[[allTouches allObjects] objectAtIndex:0] locationInView:self];
CGPoint currentTouchPosition2 = [[[allTouches allObjects] objectAtIndex:1] locationInView:self];

CGFloat currentFingerDistance = CGPointDist(currentTouchPosition1, currentTouchPosition2);
CGFloat previousFingerDistance = CGPointDist(previousTouchPosition1, previousTouchPosition2);
if (fabs(currentFingerDistance - previousFingerDistance) > ZOOM_DRAG_MIN) {
NSNumber* movedDistance = [NSNumber numberWithFloat:currentFingerDistance - previousFingerDistance];
if (currentFingerDistance > previousFingerDistance) {
NSLog(@"zoom in");
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_IN object:movedDistance];
} else {
NSLog(@"zoom out");
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_ZOOM_OUT object:movedDistance];
}
}
}
}

if (touch.phase == UITouchPhaseEnded) {
CGPoint currentTouchPosition = [touch locationInView:self];

// Check if it's a swipe
if (fabsf(startTouchPosition1.x - currentTouchPosition.x) >= SWIPE_DRAG_HORIZ_MIN &&
// fabsf(startTouchPosition1.y - currentTouchPosition.y) <= SWIPE_DRAG_VERT_MAX &&
fabsf(startTouchPosition1.x - currentTouchPosition.x) > fabsf(startTouchPosition1.y - currentTouchPosition.y) &&
touch.timestamp - startTouchTime < 0.7
) {
// It appears to be a swipe.
if (startTouchPosition1.x < currentTouchPosition.x) {
NSLog(@"swipe right");
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_RIGHT object:touch];
} else {
NSLog(@"swipe left");
[[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_SWIPE_LEFT object:touch];
}
}
startTouchPosition1 = CGPointMake(-1, -1);
_initialView = nil;
}

编辑:关于您的代码...

您没有进行正确的滑动检测。事实上,采取这一行:

    if(CurrentPosition.x >= kMinimumLength)

您正在将位置 (CurrentPosition.x) 与距离 (kMinimumLength) 进行比较。这其实意义不大。您需要什么 保存最后一次触摸位置,然后计算最后一次触摸和当前触摸之间的距离,并将此距离与 kMinimumLenght 进行比较。

我发布示例代码的原因是,通过检查一个有效的实现,您可以了解需要做什么才能检测到滑动。

关于ios - Multiview App 中的滑动手势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6598618/

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