こんにちはUIViewController
、私はUIImageViewとUIImageViewを持ってSwipeGestureRecognizer
います。したがって、ユーザーが左または右にスワイプするとchange
、画像が表示されUIImageView
ます。これまでのところ、機能しています。UIScrollView
ページングを有効にすると、画像の 1 つのサイズを使用できません。ドラッグの効果を実現し、画像UIImageView
のように次の部分UIScrollView
を表示したいと考えています。
質問する
3188 次
6 に答える
3
簡単な答え:
画像ビューに 2 つのジェスチャを追加し、カウント変数を 1 つ配置し、スワイプとノーに基づいてカウント変数を切り替えます。画像の。最後に、カウントに基づいて画像を変更します。
このコードを使用すると、正常に動作し、シンプルになります。
- (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];
}
}
于 2013-09-20T09:14:42.050 に答える
2
メインビューにスクロールビューを追加(フレームを適切に設定)
[mainview addSubview:scr];
次のようなメソッドを記述します。
- (void)setupScrollView:(UIScrollView*)scrMain {
}
次に、viewDidLoad
スクロールビューをメソッドに渡します。
[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];
}
手動で両方向に移動できます。
于 2013-09-20T09:01:13.823 に答える