문제

I'm working on a project which includes some slightly more complex dynamic layout of interface elements than what I'm used to. I always feel stupid writing complex code that checks if so-and-so is close to such-and-such and in that case move it x% in some direction, etc. That's just not how programming should be done. Programming should be as declarative as possible!

Precisely because what I'm going to do is fairly simple, I thought it would be a good opportunity to try something new, and I thought of using NSPredicate as a simple constraints solver. I've only used NSPredicate for very simple tasks so far, but I know that it capable of much more.

Are there any ideas, experiences, examples, warnings, insights that could be useful here?

I'll give a very simple example so there will be something concrete to answer. How could I use NSPredicate to solve the following constraints:

viewB.xmid = (viewB.leftEdge + viewB.width) / 2
viewB.xmid = max(300, viewA.rightEdge + 20 + viewB.width/2)

("viewB should be horizontally centered on coordinate 300, unless its left edge gets within 20 pixels of viewB's right edge, in which case viewA's left edge should stay fixed at 20 pixels to the right of viewB's right edge and viewA's horizontal center get pushed to the right.")

viewA.rightEdge and viewB.width can vary, and those are the 'input variables'.

EDIT: Any solution would probably have to use the NSExpression method -(id)expressionValueWithObject:(id)object context:(NSMutableDictionary *)context. This answer is relevant.

도움이 되었습니까?

해결책 2

Solving simple linear equations with NSPredicate (and NSExpression) is a breeze, but to go from there to solving arbitrary collections of constraints (aka Linear Programming) is still a big step. The most promising way forward is to do what Chris Hanson suggests and update his BDRuleEngine. Fun project for a rainy day.

다른 팁

i는 xep-0280 메시지 탄소 가 당신이 찾고있는 것입니다.XMPP 사이트에 따르면 프로토콜 확장은 여전히 실험 단계에 있습니다.

Ejabberd에서 이것을 지원하는 데 몇 가지 관심이있는 것처럼 보입니다.참조 :

http://mail.jabber.org/pipermail/standards/2011 년 3 월 / 024245.html http://lists.jabber.ru/pipermail/ejabberd/2012-march/007413.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top