iOS Swiping functions(IOS刷屏功能)
本文介绍了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刷屏功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:IOS刷屏功能


基础教程推荐
猜你喜欢
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01