尝试在Objective-C中创建类似下面的内容,这在
Swift中可用.
我尝试使用桥接等,将此代码用于我的Objective C项目,但它有导入问题.
在Objective-C中我尝试了下面的东西,部分它适用于UIView,但我想我需要添加转换来完成UIViewController.
我尝试了什么
- (IBAction)menuButtonPressed:(id)sender
{
if ([self.delegate respondsToSelector:@selector(menuButtonPressed)]) {
[self.delegate menuButtonPressed];
}
if (!isVertical) {
[UIView animateWithDuration: 0.6
delay: 0.0
usingSpringWithDamping: 0.3
initialSpringVelocity: .8
options: 0
animations: ^
{
self.transform = CGAffineTransformTranslate(self.transform, -240, 0);
self.transform = CGAffineTransformRotate(self.transform, 90 * M_PI / 180);
self.transform = CGAffineTransformTranslate(self.transform, 240, 0);
}
completion:^(BOOL finished) {
isVertical = YES;
}
];
}else{
[UIView animateWithDuration: 0.45
delay: 0.0
usingSpringWithDamping: 0.44
initialSpringVelocity: .8
options: 0
animations: ^
{
self.transform = CGAffineTransformTranslate(self.transform, -240, 0);
self.transform = CGAffineTransformRotate(self.transform, -90 * M_PI / 180);
self.transform = CGAffineTransformTranslate(self.transform, 240, 0);
}
completion:^(BOOL finished) {
isVertical = NO;
}
];
}
}
任何输入都会有所帮助.
最佳答案 这是一个非常简单的实现,可用于为视图控制器之间的转换设置动画.如果你想将它与UIViews一起使用,你基本上可以使用相同的动画要点.
let generateRandomColor: Void -> UIColor = {
let red = CGFloat(arc4random_uniform(255)) / 255.0
let green = CGFloat(arc4random_uniform(255)) / 255.0
let blue = CGFloat(arc4random_uniform(255)) / 255.0
return UIColor(red:red, green: green, blue: blue, alpha: 1)
}
class SlideAnimationController: NSObject, UIViewControllerAnimatedTransitioning {
let presenting: Bool
init(presenting: Bool) {
self.presenting = presenting
super.init()
}
func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
return 0.5
}
func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
let containerView = transitionContext.containerView()!
let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey)!
let toView = transitionContext.viewForKey(UITransitionContextToViewKey)!
var animatingView: UIView
var initialTransform: CGAffineTransform
var finalTransform: CGAffineTransform
if presenting {
containerView.addSubview(toView)
animatingView = toView
initialTransform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
finalTransform = CGAffineTransformIdentity
} else {
containerView.insertSubview(toView, belowSubview: fromView)
animatingView = fromView
initialTransform = CGAffineTransformIdentity
finalTransform = CGAffineTransformMakeRotation(CGFloat(-M_PI_2))
}
animatingView.layer.anchorPoint = CGPointMake(0, 0)
animatingView.frame = containerView.bounds
animatingView.transform = initialTransform
UIView.animateWithDuration(transitionDuration(transitionContext), delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 16, options: UIViewAnimationOptions.CurveLinear, animations: { () -> Void in
animatingView.transform = finalTransform
}) {
_ in
toView.layer.anchorPoint = CGPointMake(0.5, 0.5)
toView.frame = containerView.bounds
transitionContext.completeTransition(true)
}
}
}
class PresentedViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = generateRandomColor()
let tapGestureRecognizer = UITapGestureRecognizer(target: self,
action: "tapped")
view.addGestureRecognizer(tapGestureRecognizer)
}
func tapped() {
dismissViewControllerAnimated(true, completion: nil)
}
}
class TestViewController: UIViewController, UIViewControllerTransitioningDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = generateRandomColor()
let tapGestureRecognizer = UITapGestureRecognizer(target: self,
action: "tapped")
definesPresentationContext = true
view.addGestureRecognizer(tapGestureRecognizer)
}
func tapped() {
let presentedViewController = PresentedViewController()
presentedViewController.modalPresentationStyle = UIModalPresentationStyle.CurrentContext
presentedViewController.transitioningDelegate = self
presentViewController(presentedViewController, animated: true, completion: nil)
}
func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return SlideAnimationController(presenting: true)
}
func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
return SlideAnimationController(presenting: false)
}
}