本文介绍了IOS刷屏功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
日安!我有一个问题,我有一个单页应用程序的代码
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint endPosition = [touch locationInView:self.view];
if (self.imageStatus>0)
{
if (self.startPosition.x < endPosition.x)
{
// Right swipe
CATransition *transition = [CATransition animation];
transition.duration = 0.75;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
[self.view addSubview:myViewController.view];
self.imageStatus --;
if (self.imageStatus == 0)
{
self.myImage.image = [UIImage imageNamed:@"blue_back.png"];
}
else
{
self.myImage.image = [UIImage imageNamed:@"black_back.png"];
}
}
}
if (self.imageStatus<2)
{
if (self.startPosition.x > endPosition.x)
{
// Left swipe
CATransition *transition = [CATransition animation];
transition.duration = 0.75;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromRight;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
[self.view addSubview:myViewController.view];
self.imageStatus ++;
if (self.imageStatus == 2)
{
self.myImage.image = [UIImage imageNamed:@"red_back.png"];
}
else
{
self.myImage.image = [UIImage imageNamed:@"black_back.png"];
}
}
}
当我打开应用程序时,默认背景是黑色,当我向右滑动时,它会显示BLUE_BACK.png或蓝色背景,当我向后滑动时,它会显示默认背景是黑色,当再次打开黑色时,我会向左滑动它,它会显示RED_BACK.png或红色背景,反之亦然。现在我想要的是,我将在红色旁边添加另一个黄色背景的页面,所以当我在默认的黑色页面中时,当我向左滑动两次时,它会显示黄色。还有滑动功能,当我滑动它的时候,可能只需要一秒钟就可以切换一页,我如何才能实现像表格视图中那样的"实时滑动",但它是水平的,所以它快速地切换页面。谢谢!
推荐答案
更新答案:
您需要的是分页滚动视图。诀窍是添加视图在滚动视图中的正确位置,并正确设置滚动视图内容大小。具有两个视图的示例:
- (void)viewDidLoad
{
[super viewDidLoad];
self.edgesForExtendedLayout = UIRectEdgeBottom;
UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
scrollView.pagingEnabled = YES;
scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, self.view.bounds.size.height);
[self.view addSubview:scrollView];
UIView *redView = [[UIView alloc] init];
redView.frame = self.view.bounds;
redView.backgroundColor = [UIColor redColor];
[scrollView addSubview:redView];
UIView *blueView = [[UIView alloc] init];
CGRect frame = self.view.bounds;
frame.origin.x = 320;
blueView.frame = frame;
blueView.backgroundColor = [UIColor blueColor];
[scrollView addSubview:blueView];
}
以前的答案是:
我会更容易使用内置的手势识别器。特别是UIPanGestureRecognizer。
示例: 在视图中DidLoad:
UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(myAction:)];
[self.view addGestureRecognizer:pan];
和操作:
- (void) myAction:(UIPanGestureRecognizer*)gestureRec
{
if (gestureRec.state == UIGestureRecognizerStateEnded) {
NSLog(@"ended");
NSLog(@"%@", NSStringFromCGPoint([gestureRec velocityInView:self.view]));
}
}
状态决定何时按您希望的方式结束。您可以使用velocityInView来确定向右/向左和向上/向下移动
这篇关于IOS刷屏功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!