当我在模拟器上的任何地方从右向左交换时,它正在交换,但我只想在绿色视图上进行交换。 我试过这个..
class ViewController: UIViewController {
@IBOutlet weak var red: UIView!
@IBOutlet weak var green: UIView!
override func viewDidLoad() {
super.viewDidLoad()
var sswipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
sswipe.direction = .Left
view.addGestureRecognizer(sswipe)
var srwipe = UISwipeGestureRecognizer(target: self, action: Selector("handleSwipes:"))
srwipe.direction = .Right
view.addGestureRecognizer(srwipe)
}
func handleSwipes(sender:UISwipeGestureRecognizer) {
if(sender.direction == .Left) {
var labelPosition = CGPointMake(self.view.frame.origin.x - 100.0, self.view.frame.origin.y)
self.view.frame = CGRectMake(labelPosition.x , labelPosition.y , self.view.frame.size.width, self.view.frame.size.height)
}
if(sender.direction == .Right) {
var labelPosition = CGPointMake(self.view.frame.origin.x + 100.0, self.view.frame.origin.y)
self.view.frame = CGRectMake(labelPosition.x , labelPosition.y , self.view.frame.size.width, self.view.frame.size.height);
}
}
答案 0 :(得分:1)
您正在将手势添加到主视图中,您需要将其添加到所需的视图中,其绿色应该是
green.addGestureRecognizer(sswipe)
答案 1 :(得分:1)
您必须先在viewdidload中声明滑动手势
let swipeLeft = UISwipeGestureRecognizer(target: self, action: #selector(MainNotificationVC.swipeLeft))
swipeLeft.direction = .Left
ViewGreen.addGestureRecognizer(swipeLeft)
let swipeRight = UISwipeGestureRecognizer(target: self, action: #selector(MainNotificationVC.swipeRight))
swipeRight.direction = .Right
viewRed.addGestureRecognizer(swipeRight)
比之后
func swipeLeft() {
//animation
let transition: CATransition = CATransition()
transition.type = kCATransitionReveal
transition.duration = 0.5
transition.subtype = kCATransitionFromLeft
self.ViewGreen.layer.addAnimation(transition, forKey: nil)
// add your code
}
func swipeRight() {
let transition: CATransition = CATransition()
transition.type = kCATransitionReveal
transition.duration = 0.5
transition.subtype = kCATransitionFromRight
self.viewRed.layer.addAnimation(transition, forKey: nil)
}