animateWithDuration无效(iOS)

时间:2016-06-04 02:25:55

标签: ios animation

我创建了一个视图(viewToAnimate),我想让标题向上或向下动画。当我按下“向下”动作时,它向下动画很好。当我按下“向上动作时,它什么也没做。为什么会这样?为什么我不能让它在屏幕上制作动画?

Git Repo:https://github.com/joshoconnor89/animation

gb <- ggplot_build(fig3)

w = with(gb$data[[1]][1,], xmax-xmin)

for(i in 1:2) {
gb$data[[i]][gb$data[[i]]$group == 2, "x"] = 2-(w/2)
gb$data[[i]][gb$data[[i]]$group == 2, "xmin"] = 2-(w/2)-(w/2)
gb$data[[i]][gb$data[[i]]$group == 2, "xmax"] = 2-(w/2)+(w/2)

gb$data[[i]][gb$data[[i]]$group == 3, "x"] = 2+(w/2)
gb$data[[i]][gb$data[[i]]$group == 3, "xmin"] = 2+(w/2)-(w/2)
gb$data[[i]][gb$data[[i]]$group == 3, "xmax"] = 2+(w/2)+(w/2)
}

# Get the ggplot grob
gp = ggplot_gtable(gb)

library(grid)
grid.newpage()
grid.draw(gp)

//在storyboard中查看:注意它从viewcontroller上方开始 enter image description here

3 个答案:

答案 0 :(得分:0)

enter image description here错误是您使用两种方法连接上滑按钮尝试从中删除up方法的连接并让连接断开。

答案 1 :(得分:0)

问题是func myImageUploadRequest() { let myUrl = NSURL(string: "http://www.YourRestapi/postImage.php"); let request = NSMutableURLRequest(URL:myUrl!); request.HTTPMethod = "POST"; let param = [ "firstName" : "abc", "lastName" : "xyz", "userId" : "1" ] let boundary = generateBoundaryString() request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type") let imageData = UIImageJPEGRepresentation(myImageView.image, 1) if(imageData==nil) { return; } request.HTTPBody = createBodyWithParameters(param, filePathKey: "file", imageDataKey: imageData, boundary: boundary) myActivityIndicator.startAnimating(); let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in if error != nil { println("error=\(error)") return } // You can print out response object println("******* response = \(response)") // Print out reponse body let responseString = NSString(data: data, encoding: NSUTF8StringEncoding) println("****** response data = \(responseString!)") var err: NSError? var json = NSJSONSerialization.JSONObjectWithData(data, options: .MutableContainers, error: &err) as? NSDictionary dispatch_async(dispatch_get_main_queue(),{ self.myActivityIndicator.stopAnimating() self.myImageView.image = nil; }); /* if let parseJSON = json { var firstNameValue = parseJSON["firstName"] as? String println("firstNameValue: \(firstNameValue)") } */ } task.resume() } func createBodyWithParameters(parameters: [String: String]?, filePathKey: String?, imageDataKey: NSData, boundary: String) -> NSData { var body = NSMutableData(); if parameters != nil { for (key, value) in parameters! { body.appendString("--\(boundary)\r\n") body.appendString("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n") body.appendString("\(value)\r\n") } } let filename = "user-profile.jpg" let mimetype = "image/jpg" body.appendString("--\(boundary)\r\n") body.appendString("Content-Disposition: form-data; name=\"\(filePathKey!)\"; filename=\"\(filename)\"\r\n") body.appendString("Content-Type: \(mimetype)\r\n\r\n") body.appendData(imageDataKey) body.appendString("\r\n") body.appendString("--\(boundary)--\r\n") return body } func generateBoundaryString() -> String { return "Boundary-\(NSUUID().UUIDString)" } }extension NSMutableData { func appendString(string: String) { let data = string.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true) appendData(data!) } } (searchHeaderFrame)变量声明每个Action(向下和向上动作),我已经运行了这段代码并更新了你的代码,然后它为我工作,你改变了下面的代码,

CGRect

看我的输出如下,

enter image description here

希望它有用

答案 2 :(得分:0)

我强烈建议您在使用AutoLayout时为约束而不是视图框设置动画。

以下是完全符合您需要的代码:

@implementation TestViewController

- (IBAction)slideUp:(id)sender
{
    _topSpaceConstraint.constant = -_viewToAnimate.frame.size.height;

    [UIView animateWithDuration:0.5f animations:^
    {
        [self.view layoutIfNeeded];
    }];
}

- (IBAction)slideDown:(id)sender
{
    _topSpaceConstraint.constant = 0;

    [UIView animateWithDuration:0.5f animations:^
     {
         [self.view layoutIfNeeded];
     }];
}

@end

你看,它比你那里的代码简单得多。您只需使用superview更改viewToAnimate的垂直空间约束的常量值。您可以将其设置为视图高度的负值,使其完全脱离屏幕,然后将其设置为零,然后它将粘贴到屏幕的最顶部。就这么简单。

我建议你为约束而不是框架设置动画的原因是,在AutoLayout中,它是决定视图位置的约束。框架只是AutoLayout过程的结果。虽然动画框架可以起作用,但我相信在将来出现更复杂的动画时你会遇到布局问题。

希望这会有所帮助。 :)