大家好,我有一个UIViewController
和UIImageView,我有一个SwipeGestureRecognizer
。因此,如果用户向左或向右滑动,它将change
UIImageView
图像。到目前为止,它正在工作。在启用分页的情况下,我无法使用其中一个图片的大小UIScrollView
。我希望实现拖动的效果,并在图片UIImageView
中查看下一个UIScrollView
的部分会做的。
答案 0 :(得分:3)
简单回答:
为图像视图添加两个手势并放置一个计数变量,根据滑动切换计数变量,然后不。的图像。最后根据计数更改图像。
使用此代码也可以正常工作。
- (void)addAnimationPresentToView:(UIView *)viewTobeAnimated
{
CATransition *transition = [CATransition animation];
transition.duration = 0.30;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[transition setValue:@"IntroSwipeIn" forKey:@"IntroAnimation"];
transition.fillMode=kCAFillModeForwards;
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromRight;
[viewTobeAnimated.layer addAnimation:transition forKey:nil];
}
- (void)addAnimationPresentToViewOut:(UIView *)viewTobeAnimated
{
CATransition *transition = [CATransition animation];
transition.duration = 0.30;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
[transition setValue:@"IntroSwipeIn" forKey:@"IntroAnimation"];
transition.fillMode=kCAFillModeForwards;
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
[viewTobeAnimated.layer addAnimation:transition forKey:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
count = 0;
// Do any additional setup after loading the view from its nib.
[self.imageViewSwipe setUserInteractionEnabled:YES];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeRight:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[self.imageViewSwipe addGestureRecognizer:swipeLeft];
[self.imageViewSwipe addGestureRecognizer:swipeRight];
}
-(void)changeImage
{
switch (count) {
case 1:
self.imageViewSwipe.image = [UIImage imageNamed:@"Screen Shot 2013-09-17 at 10.57.51 AM.png"];
break;
case 2:
self.imageViewSwipe.image = [UIImage imageNamed:@"Screen Shot 2013-09-17 at 10.58.04 AM.png"];
break;
case 3:
self.imageViewSwipe.image = [UIImage imageNamed:@"Screen Shot 2013-09-17 at 10.58.00 AM.png"];
break;
default:
break;
}
}
-(void)handleSwipe:(id)sender
{
if(count < 4)
{
count++;
UIImageView *moveIMageView = self.imageViewSwipe;
[self addAnimationPresentToView:moveIMageView];
[self changeImage];
}
}
-(void)handleSwipeRight:(id)sender
{
if(count>0)
{
count--;
UIImageView *moveIMageView = self.imageViewSwipe;
[self addAnimationPresentToViewOut:moveIMageView];
[self changeImage];
}
}
-(void)handleSwipe:(id)sender
{
if(count < 4)
{
count++;
[self changeImage];
}
}
-(void)handleSwipeRight:(id)sender
{
if(count>0)
{
count--;
[self changeImage];
}
}
答案 1 :(得分:2)
将scroollview添加到mainview(适当设置框架)
[mainview addSubview:scr];
写一个方法,如:
- (void)setupScrollView:(UIScrollView*)scrMain {
}
然后在viewDidLoad
中将scrollview传递给方法,
[self setupScrollView:scr];
- (void)setupScrollView:(UIScrollView*)scrMain {
// we have 10 images here.
// we will add all images into a scrollView & set the appropriate size.
for (int i=1; i<=5; i++) {
// create image
// UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.png",i]];
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"sti%02i.png",i]];
// create imageView
// NSLog(@"===============>%d",i);
UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake((i-1)*scrMain.frame.size.width, 0, scrMain.frame.size.width, scrMain.frame.size.height)];
// set scale to fill
imgV.contentMode=UIViewContentModeScaleToFill;
// set image
[imgV setImage:image];
// apply tag to access in future
imgV.tag=i+1;
// add to scrollView
[scrMain addSubview:imgV];
}
// set the content size to 10 image width
[scrMain setContentSize:CGSizeMake(scrMain.frame.size.width*5, scrMain.frame.size.height)];
// enable timer after each 2 seconds for scrolling.
// [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:nil userInfo:nil repeats:YES];
}
它允许您手动移动两个方向。
答案 2 :(得分:1)
*
您可以尝试使用imageView.layer来提供一些动画效果。
*
答案 3 :(得分:1)
我认为最好的是使用UIPanGestureRecognizer
:
UIPanGestureRecognizer * pan1 = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(moveObject:)];
pan1.minimumNumberOfTouches = 1;
[image1 addGestureRecognizer:pan1];
-(void)moveObject:(UIPanGestureRecognizer *)pan;
{
image1.center = [pan locationInView:image1.superview];
}
当你开始拖动时检查方向。如果方向是从左到右,则使用-image.width将“之前”图像添加到屏幕,并在移动方法中移动此图像Before.center = [pan locationInView:image1.superview];太。如果方向是从右到左,则使用+ screensize + image.width将“下一个”图像添加到scree,并使用int move方法,移动此imageNext.center = [pan locationInView:image1.superview];太
我没试过这个,但我认为它应该有效。我希望它有所帮助
编辑:
我找到了如何检测方向:
-(void)moveObject:(UIPanGestureRecognizer *)pan;
{
CGPoint vel = [pan velocityInView:self.view];
if (vel.x > 0)
{
// user dragged towards the right
counter++;
}
else
{
// user dragged towards the left
counter--;
}
}
答案 4 :(得分:0)
-(void)panTableView:(UISwipeGestureRecognizer *)pan{
if (pan.state==UIGestureRecognizerStateEnded) {
int curr=currentPage;
if (pan==self.panGestureLeft)
{
// user dragged towards the right
CATransition *transition = [CATransition animation];
transition.duration = 0.35;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromRight;
transition.delegate = self;
[scrollview.layer addAnimation:transition forKey:nil];
}
else if (pan==self.panGestureRight)
{
// user dragged towards the left
CATransition *transition = [CATransition animation];
transition.duration = 0.35;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromLeft;
transition.delegate = self;
[scrollview.layer addAnimation:transition forKey:nil];
}
}
}
答案 5 :(得分:0)
实施水平表滚动
创建TableView并转换表
self.horizontalTableView.transform = CGAffineTransformMakeRotation(-M_PI * 0.5);
然后,您将反转应用于单元格内的内容。
CGAffineTransformMakeRotation(M_PI * 0.5)
如果需要,可以将其放入自定义UITableViewCell内的另一个TableView中,并有一个垂直的水平表表。
您可以获得Native Scrolling的所有好处
参见本教程: