我有一个视图(A),其中包含几个矩形子视图(B)。这些子视图中的每一个都有一个点击识别器来触发一个动作。父视图 A 也有一个单击识别器,它调用 A 控制器上的一个函数,使每个子视图 B 以一种颜色闪烁。这是函数:
@IBAction func highlightAreas(recognizer: UITapGestureRecognizer) {
for area in buttons {
// only show linked areas
if area.targetPage != nil {
let oldColor = area.backgroundColor
// show areas with animation
UIView.animateWithDuration(HIGHLIGHT_ANIMATION_TIME, animations: { Void in // begin of closure
area.backgroundColor = self.HIGHLIGHT_BACKGROUND_COLOR.colorWithAlphaComponent(self.HIGHLIGHT_ALPHA)
}) // end of closure
// hide areas with animation
UIView.animateWithDuration(HIGHLIGHT_ANIMATION_TIME, animations: { Void in // begin of closure
area.backgroundColor = oldColor?.colorWithAlphaComponent(0.0)
}) // end of closure
}
}
}
Run Code Online (Sandbox Code Playgroud)
它有效,但在动画期间子视图 B …
到目前为止,我创建了自己的自定义视图,它是 UIView 类的子类:
class MyConnections: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func drawRect(rect: CGRect) {
// Drawing code
let context = UIGraphicsGetCurrentContext()
CGContextSetLineWidth(context, 1)
CGContextSetStrokeColorWithColor(context, UIColor.blackColor().CGColor)
let circle = CGRectMake(5, 60, 80, 80)
CGContextAddEllipseInRect(context, circle)
CGContextStrokePath(context)
CGContextSetFillColorWithColor(context, UIColor.whiteColor().CGColor)
CGContextFillEllipseInRect(context, circle)
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的视图控制器,我将上面的视图添加为子视图:
let profile = MyConnections()
override func …Run Code Online (Sandbox Code Playgroud) 我有一个 UICollectionViewController 并且我添加了两个 UIView 作为子视图。一个是紫色的 UIView,上面是另一个白色的 UIView,蓝色的集合视图在两者后面向上滚动。
在这些 UIViews 下方,我collectionView.contentInset从前 300 个(这是两个 UIViews 高度的总大小)制作了。我想要完成的是滚动集合视图以及上面的两个 UIView。它几乎类似于此线程上的解决方案(在 UITableView 中滚动时移动视图),除了当我覆盖 scrollViewDidScroll 时,整个框架会向上滚动并且单元格位于两个视图后面。我想要的只是向上滚动 UIViews,然后滚动浏览集合视图。我觉得这可能涉及嵌套滚动视图。
这就是我如何覆盖 scrollViewDidScroll:
var rect = self.view.frame
rect.origin.y = -scrollView.contentOffset.y - 300
self.view.frame = rect
Run Code Online (Sandbox Code Playgroud)
编辑:我发布了一个视频,演示了我想在每个 iOS Tumblr 应用程序中执行的操作:https : //youtu.be/elfxtzoiHQo
我在中创建了一个 menuView self.view,menuView 有两个子视图,它们是 UIView。此 UIView 包含 4 个 UIButton ,但我无法单击其中任何一个。userInteractionEnabled设置是,按钮的框架在视图调试中设置正确。代码:
更新:Github 中的项目
视图控制器.m:
- (void)viewDidLoad {
[super viewDidLoad];
LeftMenuView *leftView=[LeftMenuView createLeftMenuView];
leftView.userInteractionEnabled=YES;
self.leftView=leftView;
[self.view addSubview:leftView];
//nightButton
UIButton *nightButton=[self.leftView viewWithTag:6];
[nightButton addTarget:self action:@selector(touchButton:) forControlEvents:UIControlEventTouchUpInside];
leftView.userInteractionEnabled=YES;
//set
UIButton *settingButton=[self.leftView viewWithTag:4];
[settingButton addTarget:self.leftView action:@selector(push) forControlEvents:UIControlEventTouchUpInside];
}
Run Code Online (Sandbox Code Playgroud)
**LeftMenuView.m:**这是一个 UIView 类。这是我设置按钮和菜单的地方。
-(instancetype)initWithFrame:(CGRect)frame
{
self=[super initWithFrame:frame];
self.backgroundColor=[UIColor colorWithRed:26/256.f green:31/256.f blue:36/256.f alpha:0.7];
[self addHeaderView];
[self addButton];
return self;
}
#pragma mark -add view to menu view
-(void)addHeaderView
{
UIView *headViewUp=[[UIView alloc]init];
headViewUp.userInteractionEnabled=YES; …Run Code Online (Sandbox Code Playgroud) 我有一个 UIView 有一个 UITableView 子视图。
UIView 是一个菜单,初始宽度为设备宽度的 50%,UITableView 对 UIView 具有 AutoLayout 约束。
在菜单中选择一个项目会触发 UIView 将大小调整为设备宽度的 33%。UITableView,正如我所料,在没有动画的情况下改变大小,然后 UIView 通过动画赶上它。
有没有什么简单的方法可以用我的 UIView 为我的 UITableView 设置动画,同时保持 AutoLayout 约束?
我在与 UIView 相同的动画块中尝试了以下操作:
let tableWidth = self.mm.menuOneController.tableView.bounds.size.width
let newTableWidth = (tableWidth * 2) / 3
self.mm.menuOneController.tableView.bounds.size.width = newTableWidth
Run Code Online (Sandbox Code Playgroud)
这是有效的,UITableView 与 UIView 一起随着动画调整大小,但是 AutoLayout 并不像我期望的那样工作。
UIView 的 X 值是 0.0,所以它被固定在超级视图的左边,当它调整大小时,它是从右手大小开始的。但是, UITableView 似乎在左右调整大小,直到达到正确的宽度(我希望这是有道理的)。
有什么建议?
提前致谢!
我看过很多类似的帖子,但没有一个有效。
到目前为止,这是我的代码。
func startRotating(view : UIImageView, rotating : Bool) {
//Rotating the rotatingClockBarImage
UIView.animate(withDuration: 1.0, delay: 0.0, options: [.curveLinear], animations: {
view.transform = CGAffineTransform(rotationAngle: CGFloat.pi)
}, completion: {finished in
UIView.animate(withDuration: 1.0, delay: 0.0, options: [.curveLinear], animations: {
view.transform = CGAffineTransform(rotationAngle: 0)
}, completion: nil)
})//End of rotation
continueRotating(view: view)
}
Run Code Online (Sandbox Code Playgroud)
最初的问题是我无法旋转完整的 360 度。我通过在完成时旋转一半和另一半来解决这个问题。
现在的问题是一旦这个动画完成,就是这样。我试过把它放在一个 while 循环中,for 循环,来回调用两个类似的函数。没有任何效果,它只是一直冻结我的应用程序。
例如,在 for 循环中,将运行 3 次,我放置了一个 print()。打印写入控制台三次,但动画只发生一次。正因为如此,我认为动画只是在它开始之前就被切断了,最后的旋转是唯一播放的。所以我需要找到一种方法让它播放每个旋转。
看到 Apple 的飞机在游戏应用程序中的旧版本 Xcode 中如此轻松地旋转,这不应该是那么难。我试图避免删除并重新安装旧版本,以便我可以查看该代码。
我正在实现这样的旋转数字视图:https : //github.com/jonathantribouharet/JTNumberScrollAnimatedView
但是,不是从 1 滚动到 9,我希望动画继续滚动(1 到 9,然后回到 1 到 9),直到我手动停止它。
我通过添加 UILabel 子视图 1-9、垂直堆叠并在底部为数字 1 添加一个额外的标签来实现这一点。旋转到底部时(显示底部标签1),在动画完成闭包中,我将变换设置回原点,显示顶部标签1,并重新启动旋转动画。
这工作正常,但由于每个旋转 (1-9-1) 是独立的,因此 1-9 的动画会加速,然后在 1 处停止,并在下一次迭代中加快速度。这不是我想要的,我希望旋转在迭代之间保持恒定速度。
我怎样才能用 UIView 动画做到这一点?
这是代码:
public func startAnimation(){
UIView.setAnimationCurve(UIViewAnimationCurve.easeIn)
UIView.animate(withDuration: TimeInterval(self.loopDuration), animations: { [unowned self] in
self.container!.transform = CGAffineTransform(translationX: 0, y: CGFloat(-(self.endNumber-self.startNumber+1)*(self.size)))
}) { [unowned self] (completed) in
if self.isAnimationStopped {
self.container!.transform = CGAffineTransform(translationX: 0, y: 0)
UIView.setAnimationCurve(UIViewAnimationCurve.easeOut)
UIView.animate(withDuration: TimeInterval(self.loopDuration), animations: {
self.container!.transform = CGAffineTransform(translationX: 0, y: CGFloat(-self.targetY))
})
} else {
self.container!.transform = CGAffineTransform(translationX: …Run Code Online (Sandbox Code Playgroud) 为 ProgressView 使用动画时,如果我让动画用完,它会完全正常,但是如果我在动画仍在运行时再次尝试运行此代码,它将向当前条添加 100% 并使其通过条. 图像应该更合乎逻辑地解释情况。
进度从 1.0 (100%) 开始:
进展已经进行了一半:
代码再次运行,导致进度超过 100%,尽管它使用了正确的时间来完成。
这是使用的代码:
self.progressView.setProgress(1.0, animated: false)
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
UIView.animate(withDuration: 10, delay: 0, options: [.beginFromCurrentState, .allowUserInteraction], animations: { [unowned self] in
self.progressView.setProgress(0, animated: true)
})
}
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我正在制作一个天气应用程序,为了显示当天的最高和最低温度,我使用了一个 UIView,它是我使用 .layer.cornerRadius 圆角的角落。现在我想添加一个具有不同颜色(象征温度)的渐变层,但没有出现gradientLayer。这是我的 viewDidLoad(),视图名称是 currentMinMaxTemperatureView:
let gradientLayer = CAGradientLayer()
gradientLayer.frame = currentMinMaxTemperatureView.bounds
gradientLayer.cornerRadius = currentMinMaxTemperatureView.cornerRadius
gradientLayer.colors = [UIColor.blue, UIColor.yellow, UIColor.red]
currentMinMaxTemperatureView.layer.addSublayer(gradientLayer)
Run Code Online (Sandbox Code Playgroud)
为什么不出现?运行此程序时,我的视图如下所示:
我有一个视图控制器,它显示在 a 中,UIView因为我正在使用CarbonKit制作类似 android 的选项卡。
我有一个userProfileViewController我在其中一个选项卡下显示的,这是UIImageView我试图通过使用以下代码在圆圈中显示的:
self.userDisplayPic.layer.cornerRadius = self.userDisplayPic.frame.size.width / 2;
self.userDisplayPic.clipsToBounds = true
Run Code Online (Sandbox Code Playgroud)
我的猜测是,这可能是因为在内部显示时自动调整大小UIView可能会压缩设计,但其他元素特别是更新按钮似乎很好。所以有什么建议可以解决这个问题吗?
uiview ×10
ios ×8
swift ×7
animation ×2
gradient ×1
objective-c ×1
rotation ×1
swift3 ×1
swift4 ×1
uiimageview ×1
uiscrollview ×1
uitableview ×1
xcode ×1