Как я тяну тень под UIView?

Нечто подобное может сделать это

library("rjson")
json_file <- "https://api.coindesk.com/v1/bpi/currentprice/USD.json"

numOfTimes <- 2L # how many times to run in total
sleepTime <- 60L  # time to wait between iterations (in seconds)
iteration <- 0L
while (iteration < numOfTimes) {
  # gather data
  json_data <- fromJSON(paste(readLines(json_file), collapse=""))
  # get json content as data.frame
  x = data.frame(json_data$time$updated,json_data$time$updatedISO,json_data$time$updateduk,json_data$bpi$USD)
  # create file to save in 'C:/Myfolder' 
  # alternatively, create just one .csv file and update it in each iteration
  nameToSave <- nameToSave <- paste('C:/Myfolder/', 
                                     gsub('\\D','',format(Sys.time(),'%F%T')), 
                                    'json_data.csv', sep = '_')
  # save the file
  write.csv(x, nameToSave)
  # update counter and wait 
  iteration <- iteration + 1L
  Sys.sleep(sleepTime)
}    

Обратите внимание, что для этого требуется открыть сеанс R (можно создать файл .exe или .bat и запустить его в фоновом режиме) ).

347
задан Venk 1 June 2013 в 01:17
поделиться

3 ответа

В своем текущем коде вы сохраняете GState текущего контекста, настраиваете его для рисования тени ... и восстанавливаете его до того, что было до того, как вы настроили его для рисования тени. Затем, наконец, вы вызываете реализацию суперкласса drawRect :.

Любой рисунок, на который должна влиять установка тени, должен произойти после

CGContextSetShadow(currentContext, CGSizeMake(-15, 20), 5);

, но до

CGContextRestoreGState(currentContext);

Так что если вы хотите суперкласс s drawRect: быть «обернутым» в тень, тогда как насчет того, чтобы перестроить свой код следующим образом?

- (void)drawRect:(CGRect)rect {
    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGContextSaveGState(currentContext);
    CGContextSetShadow(currentContext, CGSizeMake(-15, 20), 5);
    [super drawRect: rect];
    CGContextRestoreGState(currentContext);
}
95
ответ дан 23 November 2019 в 00:28
поделиться

Можно использовать это Extension для добавления тени

extension UIView {

    func addShadow(offset: CGSize, color: UIColor, radius: CGFloat, opacity: Float)
    {
        layer.masksToBounds = false
        layer.shadowOffset = offset
        layer.shadowColor = color.cgColor
        layer.shadowRadius = radius
        layer.shadowOpacity = opacity

        let backgroundCGColor = backgroundColor?.cgColor
        backgroundColor = nil
        layer.backgroundColor =  backgroundCGColor
    }
}

, можно назвать ее как

your_Custom_View.addShadow(offset: CGSize(width: 0, height: 1), color: UIColor.black, radius: 2.0, opacity: 1.0)
0
ответ дан 23 November 2019 в 00:28
поделиться

Тень Эскиза Используя IBDesignable и IBInspectable в Быстром 4

, КАК ИСПОЛЬЗОВАТЬ ЭСКИЗ IT

DEMO

И XCODE РЯДОМ

Shadow Exampl

, КОД

@IBDesignable class ShadowView: UIView {

    @IBInspectable var shadowColor: UIColor? {
        get {
            if let color = layer.shadowColor {
                return UIColor(cgColor: color)
            }
            return nil
        }
        set {
            if let color = newValue {
                layer.shadowColor = color.cgColor
            } else {
                layer.shadowColor = nil
            }
        }
    }

    @IBInspectable var shadowOpacity: Float {
        get {
            return layer.shadowOpacity
        }
        set {
            layer.shadowOpacity = newValue
        }
    }

    @IBInspectable var shadowOffset: CGPoint {
        get {
            return CGPoint(x: layer.shadowOffset.width, y:layer.shadowOffset.height)
        }
        set {
            layer.shadowOffset = CGSize(width: newValue.x, height: newValue.y)
        }

     }

    @IBInspectable var shadowBlur: CGFloat {
        get {
            return layer.shadowRadius
        }
        set {
            layer.shadowRadius = newValue / 2.0
        }
    }

    @IBInspectable var shadowSpread: CGFloat = 0 {
        didSet {
            if shadowSpread == 0 {
                layer.shadowPath = nil
            } else {
                let dx = -shadowSpread
                let rect = bounds.insetBy(dx: dx, dy: dx)
                layer.shadowPath = UIBezierPath(rect: rect).cgPath
            }
        }
    }
}

ПРОИЗВЕЛ

DEMO OUTPUT

0
ответ дан 23 November 2019 в 00:28
поделиться