当我使用此代码在UIImageView上滑动时,我正在尝试使用NSLog,但由于某些原因它无法正常工作。有什么想法吗?
@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");
}
答案 0 :(得分:103)
启用 UIImage查看用户互动,默认情况下已禁用。
[imageView setUserInteractionEnabled:YES];
添加滑动手势事件
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];
处理滑动手势事件
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
NSLog(@"Left Swipe");
}
if (swipe.direction == UISwipeGestureRecognizerDirectionRight) {
NSLog(@"Right Swipe");
}
}
答案 1 :(得分:9)
确保在创建UIImageView后添加imageView.userInteractionEnabled = YES;
。
这允许用户与您的视图进行交互,例如点按,拖动,滑动或其他常规手势。
请参阅文档here。
答案 2 :(得分:7)
在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")
}
}
答案 3 :(得分:3)
与其他UIViews
不同,UIImageView
默认情况下会附带用户互动disabled
。在代码中包含此行,手势应按预期工作:
imageView.userInteractionEnabled = YES;
答案 4 :(得分:2)
使用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
}
}
答案 5 :(得分:0)
我遇到了麻烦,我可以让UISwipeGestureRecognizer工作,如果我将它添加到self.view,但不是单独的UIImageView。感谢Jeff Ayan的Swift 3代码示例。 如果使用界面构建器,还可以在属性检查器下检查UIImageView的“用户交互已启用”框。