ios - IOS中的滑动手势识别
<p><p>我是 iOS 新手。</p>
<p>我正在开发一个有 2 个 <code>NSMutableArray</code> 的应用程序。我想将数组图像加载到 <code>ImageView</code>。</p>
<p>我想拍摄第一张图片并将其添加到 <code>ImageView</code>。当使用向左滑动时,我想将数组中的下一个图像添加到新的 <code>ImageView</code> 并显示它。如果他们向后滑动,请加载以前的 e.t.c</p>
<p>当用户双击图像时,我还想将第二个数组中的图像加载到 <code>ImageView</code> 中。</p>
<p>我试过这个</p>
<pre><code>AppDelegate * delegate=(AppDelegate *)[delegate];
NSLog(@"%lu",(unsigned long));
ImgView= [ initWithFrame:CGRectMake(0, 0, 320, 480)];
//ImgView.image=;
int random=arc4random()%;
NSLog(@"%d",random);
ImgView.image=;
;
currentImageIndex = 0;
ImgView.userInteractionEnabled = YES;
UISwipeGestureRecognizer *recognizer;
recognizer = [ initWithTarget:self action:@selector(handleSwipeFrom:)];
;
[ addGestureRecognizer:recognizer];
;
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer
{
NSLog(@"Swipe received.");
AppDelegate * delegate=(AppDelegate *)[delegate];
for(int i=0;i<52;i++)
{
NSLog(@"%d",i);
ImgView.image=;
}
ImgView.image=;
}
</code></pre>
<p>谁能帮我做这件事?提前谢谢你。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>几种方法:</p>
<ol>
<li><p>滑动手势:如果你想编写自己的滑动手势,你可以这样做:</p>
<ul>
<li><p>不仅定义图像名称数组的属性,还定义一个索引,显示您正在查看的卡片:</p>
<pre><code>@property (nonatomic, strong) NSMutableArray *imageNames;
@property (nonatomic) NSInteger imageIndex;
</code></pre> </li>
<li><p>将手势添加到您的 ImageView 中:</p>
<pre><code>UISwipeGestureRecognizer *swipe;
// by the way, if not using ARC, make sure to add `autorelease` to
// the following alloc/init statements
swipe = [ initWithTarget:self action:@selector(handleSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
;
swipe = [ initWithTarget:self action:@selector(handleSwipe:)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;
;
</code></pre> </li>
<li><p>编写手势识别器处理程序:</p>
<pre><code>- (void)handleSwipe:(UISwipeGestureRecognizer *)gesture
{
UIViewAnimationOptions option = kNilOptions;
if (gesture.direction == UISwipeGestureRecognizerDirectionRight)
{
// if we're at the first card, don't do anything
if (self.imageIndex == 0)
return;
// if swiping to the right, maybe it's like putting a card
// back on the top of the deck of cards
option = UIViewAnimationOptionTransitionCurlDown;
// adjust the index of the next card to be shown
self.imageIndex--;
}
else if (gesture.direction == UISwipeGestureRecognizerDirectionLeft)
{
// if we're at the last card, don't do anything
if (self.imageIndex == ( - 1))
return;
// if swiping to the left, it's like pulling a card off the
// top of the deck of cards
option = UIViewAnimationOptionTransitionCurlUp;
// adjust the index of the next card to be shown
self.imageIndex++;
}
// now animate going to the next card; the view you apply the
// animation to is the container view that is holding the image
// view. In this example, I have the image view on the main view,
// but if you want to constrain the animation to only a portion of
// the screen, you'd define a simple `UIView` that is the dimensions
// that you want to animate and then put the image view inside
// that view, and replace the `self.view` reference below with the
// view that contains the image view.
[UIView transitionWithView:self.view
duration:0.5
options:option
animations:^{
self.imageView.image = ];
}
completion:nil];
}
</code></pre> </li>
</ul></li>
<li><p>显然,如果您想编写自己的双击手势,请继续为此创建一个手势以及该手势的处理程序,例如:</p>
<pre><code>UITapGestureRecognizer *tap = [ initWithTarget:self action:@selector(handleTap:)];
tap.numberOfTapsRequired = 2;
;
</code></pre> </li>
<li><p>正如其他人所建议的,如果您不想像上面那样编写自己的图像处理代码,您可以使用各种其他控件,例如<code>UIPageViewController</code>、<code>UICollectionView</code> 或其他十几个便于在一组图像之间导航的类中的任何一个。</p></li>
</ol></p>
<p style="font-size: 20px;">关于ios - IOS中的滑动手势识别,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/16607898/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/16607898/
</a>
</p>
页:
[1]