表格视图中的UIView过渡 [英] UIView Transition in table view

查看:98
本文介绍了表格视图中的UIView过渡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在表视图单元格内进行视图的转换。更准确地说,当用户单击UITableViewCell中的按钮时,视图应从单元格的右侧滑入,并且当用户单击滑动视图中的按钮时,该视图应滑回到其原始位置。除了将滑动的视图发送回其原始位置的动画外,我可以做所有这一切。

I want to do transition of a view inside a table view cell. More precisely, when user clicks on a button in UITableViewCell a view should slide in from the right side of the cell and this view should slide back to its original position when user clicks on a button in the slided view. I could do all of this except the animation of sending the slided view back to its original position.

要完成此操作,请创建一个xib文件对于包含两个并排视图的表格视图单元,将其命名为view1和view2。将tableview加载到屏幕后,只有view1对用户可见。并将view1.trailingedge和view2.leadedge的自动布局约束设置为0,该约束连接到cell.swift并命名为leftConstraint。现在,当单击view1中的按钮时,view2使用以下代码从单元格的右侧滑动:-

To accomplish this what I have done so far is, created a xib file for the table view cell which contains two views side by side, let it be view1 and view2. When the tableview is loaded to screen, only view1 is visible to the user. And set auto layout constraint for view1.trailingedge and view2.leadingedge as 0, the constraint is connected to cell.swift and named leftConstraint. Now when a button in view1 is clicked view2 slides in from the right side of the cell using the following code :-

self.view2.transform = CGAffineTransformMakeTranslation(UIScreen.mainScreen().bounds.width, 0)
UIView.animateWithDuration(0.4, animations: { () -> Void in
    self.view2.transform = CGAffineTransformIdentity
    self.leftConstraint.constant -= UIScreen.mainScreen().bounds.width
})

当尝试将view2发送回其旧位置时(应该在单击view2中的按钮时发生)。单元格动画的右侧不起作用,view2中的所有元素都消失了。使用以下代码将视图发送到单元格的右侧:-

When tried to send back view2 to its old position (this should happen on the click of a button in view2) ie. right side of the cell animation is not working, all the elements in view2 just disappears. Using the following code for sending view to the right side of the cell:-

self.view2.transform = CGAffineTransformMakeTranslation(UIScreen.mainScreen().bounds.width, 0)
UIView.animateWithDuration(0.4, animations: { () -> Void in
   self.view2.transform = CGAffineTransformIdentity
   self.leftConstraint.constant += UIScreen.mainScreen().bounds.width
})

在此先感谢任何帮助。特别感谢@MarkHim的帮助。

Thanks in Advance for any help. And special Thanks to @MarkHim for helping.

推荐答案

由于您需要两次对rightOut位置进行转换,因此首先需要对in-动画以及稍后的out动画,您可以将其保存在 rightOutTransform 这样的变量中。尝试:

Since you need the transformation for the rightOut position twice, first for the in-animation and later for the out animation, you could save it in a variable like rightOutTransform. Try:

CGAffineTransform rightOutTransform = CGAffineTransformMakeTranslation(self.view.fame.size.width, 0);
// assuming view2 is currently in the cell
UIView.animateWithDuration(0.4, animations: {
   self.view2.transform = rightOutTransform //
})

动画制作完成后,您可能想考虑从视图层次结构中删除不再需要的视图

You might want to think about removing views you don't need anymore from the view hierarchy like this when the animation completed

view2.removeFromSuperview();

这篇关于表格视图中的UIView过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆