[英]Gesture recognizer (swipe) on UIImageView
I am trying to NSLog when I swipe over an UIImageView with this code, but it does not work for some reason. 当我使用此代码在UIImageView上滑动时,我尝试使用NSLog,但是由于某些原因它不起作用。 Any idea ?
任何的想法 ?
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"image2.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.frame = [[UIScreen mainScreen] bounds];
[self.view addSubview:imageView];
UISwipeGestureRecognizer *recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipeHandle:)];
[recognizer setNumberOfTouchesRequired:1];
[imageView addGestureRecognizer:recognizer];
}
- (void)rightSwipeHandle:(UISwipeGestureRecognizer*)gestureRecognizer {
NSLog(@"right swipe");
}
Enable UIImage view user interaction which is disabled by default. 启用默认情况下禁用的UIImage视图用户交互 。
[imageView setUserInteractionEnabled:YES];
Adding a Swipe Gesture Events 添加滑动手势事件
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];
// Setting the swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
// Adding the swipe gesture on image view
[imageView addGestureRecognizer:swipeLeft];
[imageView addGestureRecognizer:swipeRight];
Handling Swipe Gesture Events 处理滑动手势事件
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Left Swipe");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"Right Swipe");
}
}
Be sure to add imageView.userInteractionEnabled = YES;
确保添加
imageView.userInteractionEnabled = YES;
after you create your UIImageView. 创建UIImageView之后。
This allows users to interact with your view such as a tap, drag, swipe or other general gestures. 这使用户可以与您的视图进行交互 ,例如轻击,拖动,滑动或其他常规手势。
In Swift 3 在Swift 3中
ImageView.isUserInteractionEnabled = true
let swipeGesture = UISwipeGestureRecognizer(target: self, action: #selector(self.getSwipeAction(_:)))
self.ImageView.addGestureRecognizer(swipeGesture)
func getSwipeAction( _ recognizer : UISwipeGestureRecognizer){
if recognizer.direction == .right{
print("Right Swiped")
} else if recognizer.direction == .left {
print("Left Swiped")
}
}
Different than other UIViews
, UIImageView
, as default, comes with user interaction disabled
. 与其他
UIViews
不同, UIImageView
默认情况下disabled
了用户交互功能。 Include this line in your code and the gesture should work as expected: 在您的代码中包含此行,并且手势应按预期工作:
imageView.userInteractionEnabled = YES;
Its using swift 4, images are picked from your photo library can be swiped over the image view using swipe gesture. 它使用swift 4,可以使用滑动手势从照片库中拾取图像,然后在图像视图上滑动。
@IBOutlet weak var Imageview: UIImageView!
var images=[UIImage]()
var i=Int()
override func viewDidLoad() {
super.viewDidLoad()
let swipeLeftGesture=UISwipeGestureRecognizer(target: self, action: #selector(SwipeLeftImage))
Imageview.isUserInteractionEnabled=true
swipeLeftGesture.direction = UISwipeGestureRecognizerDirection.left
Imageview.addGestureRecognizer(swipeLeftGesture)
let swipeRightGesture=UISwipeGestureRecognizer(target: self, action: #selector(SwipeRightImage))
swipeRightGesture.direction = UISwipeGestureRecognizerDirection.right
Imageview.addGestureRecognizer(swipeRightGesture)
}
@objc func SwipeLeftImage(){
if i<images.count-1{
i+=1
Imageview.image=images[i]
}
}
@objc func SwipeRightImage(){
if i<=images.count-1{
i-=1
Imageview.image=images[i]
}
}
//MARK:- imagePickerController Delegate Function
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
self.dismiss(animated: true, completion: nil)
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
Imageview.contentMode = .scaleToFill
Imageview.image=nil
images.insert(pickedImage, at: images.endIndex)
Imageview.image = pickedImage
}
}
I was running into trouble with this where I could get the UISwipeGestureRecognizer to work if I added it to self.view, but not to the individual UIImageView. 我遇到了麻烦,如果我将UISwipeGestureRecognizer添加到self.view中,而不是将其添加到单个UIImageView中,则可以正常工作。 Thanks Jeff Ayan for the Swift 3 code example.
感谢Jeff Ayan提供的Swift 3代码示例。 If you use interface builder, you can also check the User Interaction Enabled box for the UIImageView under the attributes inspector.
如果使用界面生成器,则还可以在属性检查器下选中UIImageView的“启用用户交互”框。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.